-
Notifications
You must be signed in to change notification settings - Fork 230
Add retry logic for initial parameters in ExternalSampler (#2739) #2740
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
base: main
Are you sure you want to change the base?
Conversation
|
Hi @0AnshuAditya0, thanks for your interest in contributing to Turing.jl. Some of our team is currently on leave, and I'm really busy and will soon go on leave, so it may be early January before we'll have time to give your PR a proper look. So apologies if we aren't the most responsive right now, we'll get to it in a few weeks. Some quick first thoughts though:
|
|
Thanks for the feedback @mhauru! I appreciate the guidance on refactoring this properly. Your approach makes a lot of sense. Instead of duplicating the retry logic, I can extract it into a shared utility that handles the general case. Here's what I'm thinking: Proposed changes:
The shared function would handle:
Does this implementation plan align with what you had in mind? Happy to adjust the approach if you have other preferences. I'll work on these changes over the next few days. No rush on the review - I understand the holiday season timing. Take your time, and I'll have the refactored version ready when you're back! |
- Created src/mcmc/initial_params.jl with flexible validator-based approach - Refactored HMC to use shared function (removes code duplication) - Refactored external_sampler with gradient checking validation - Added comprehensive tests in test/mcmc/initial_params.jl - Tests cover retry logic, difficult initialization, and error messages Addresses feedback from @mhauru in PR TuringLang#2740: 1. ✓ Created shared function to avoid duplication 2. ✓ Made gradient checking configurable via validator pattern 3. ✓ Added tests with models that produce invalid params The validator pattern allows each sampler to define custom validation logic while sharing retry infrastructure, error handling, and attempt counting.
|
Hi @mhauru! I've implemented the changes you suggested. Here's what I did: Changes Made1. Created Shared Function
2. Made Gradient Checking Configurable
3. Added Tests
Why Validator Pattern?I chose this approach because HMC and external samplers validate parameters differently:
The validator pattern keeps them flexible while sharing the retry logic. Let me know if a simpler approach would be better! TestingTested with constrained models locally - the retry logic works as expected. Please let me know if you'd like me to change anything. Thanks for the guidance on this! |
|
Thank you for the PR @0AnshuAditya0, I have a number of comments above 🙂 |
|
All feedback addressed! Here's what I changed: ✅ Moved Ready for re-review! Thanks for the detailed feedback. |
- Moved find_initial_params from initial_params.jl to abstractmcmc.jl - Simplified validator to return Bool only (removed diagnostics) - Updated HMC and external_sampler validators accordingly - Moved tests to test/mcmc/abstractmcmc.jl - Added counter-based test for warning at attempt 10 - Added direct find_initial_params unit tests - Removed duplicate warning test from test/mcmc/hmc.jl - Removed StableRNG usage where not needed - Deleted initial_params.jl files (function and tests) Addresses all feedback
penelopeysm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking a lot nicer. More comments below.
Could I also point out, that if I wanted to talk to Claude and ask it to do this PR, I would just ask Claude to do it? I think you are smart enough to use your own words and your own programming skills to work on this. Bear in mind that people are spending real time to review your PRs.
|
thanks for the feedback. The change removes a random-value assertion and fixes the RNG seed so the test result is stable and not dependent on sampler randomness. The intent was to keep the behavior the same while making the test deterministic. I’m still learning and will keep improving how I communicate changes. Thanks for taking the time to review this. |
This reverts commit fafdf93.
|
i reverted the commit while trying to simplify the tests |
|
The test in the earlier commit was fine before you reverted it. No don't use abs(x) please, the Ref counter was exactly what's needed. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2740 +/- ##
===========================================
- Coverage 86.51% 55.74% -30.78%
===========================================
Files 20 20
Lines 1261 1270 +9
===========================================
- Hits 1091 708 -383
- Misses 170 562 +392 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
penelopeysm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are still other things from my previous review which were in the reverted commit.
Fixes #2739
This PR adds retry logic when generating initial parameters for external samplers, similar to what HMC already does.
Changes:
src/mcmc/external_sampler.jlto attempt finding valid initial parameters up to 10 timesWhy:
External samplers previously only tried once to generate initial parameters. If the first attempt produced infinite logp or gradient, sampling would fail immediately. This makes external samplers more robust.