FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Compile Time Theta Default by SteveBronder · Pull Request #3213 · stan-dev/math · GitHub

/ math Public

Compile Time Theta Default - #3213

Merged
WardBrian merged 5 commits into
laplace/move-theta-to-tolsfrom
laplace/compile-time-theta-default
Jun 30, 2025
Merged

Compile Time Theta Default#3213
WardBrian merged 5 commits into
laplace/move-theta-to-tolsfrom
laplace/compile-time-theta-default

Conversation

SteveBronder commented Jun 30, 2025
edited
Loading

Copy link
Copy Markdown
Collaborator

Summary

@WardBrian how do you feel about this alternative to #3209 ? Instead of an std::optional for theta_0 I added a template to laplace_options for the type of theta_0. Since we know at compile time whether we are using the default theta_0 we can save having to make an optional here.

Tests

No new tests

Checklist

  • Copyright holder: Simons Foundation

    The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
    - Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
    - Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

  • the basic tests are passing

    • unit tests pass (to run, use: ./runTests.py test/unit)
    • header checks pass, (make test-headers)
    • dependencies checks pass, (make test-math-dependencies)
    • docs build, (make doxygen)
    • code passes the built in C++ standards checks (make cpplint)
  • the code is written in idiomatic C++ and changes are documented in the doxygen

  • the new changes are tested

WardBrian left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

My overall thoughts are pretty neutral. It's nice that this avoids a runtime conditional, but it's only one conditional at the start of the optimization. On the other hand, I find the laplace_options_base struct much harder to read now, as we really don't semantically mean for it to be a templated struct, we just want it to either have or not-have the last member.

Comment on lines +517 to +521
if constexpr (is_eigen_v<ThetaVec>) {
return options.theta_0;
} else {
return Eigen::VectorXd::Zero(theta_size);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thoughts on making this a member function of the options struct? so this call would just be options.initial_theta(theta_size);, and the function body would basically be this code (plus maybe an assert that the size is really correct and/or moving the above checks into this function)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

If we used it more than once I would say yes, but it is only used here so I'd rather just have the code here

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

IMO it's less about the fact that it's used once and more about the fact that the immediately-evaluated-lambda trick is messy. But, I could live with it here

SteveBronder marked this pull request as ready for review June 30, 2025 20:45
Comment on lines +50 to +62
template <typename Theta, typename = void>
struct laplace_options;

/* Initial value for theta. Defaults to 0s of the correct size if nullopt */
std::optional<Eigen::VectorXd> theta_0{std::nullopt};
template <typename Theta>
struct laplace_options<Theta, require_eigen_t<Theta>> : public laplace_options_base {
/* Value for user supplied initial theta */
Theta theta_0{0};
};

template <typename Theta>
struct laplace_options<Theta, require_not_eigen_t<Theta>> : public laplace_options_base {};

using laplace_options_default = laplace_options<void>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Would it be possible just to have this be template <bool HasInitTheta> and then the two specializations just being false and true?

Something like

template <bool HasInitTheta>
struct laplace_options;

template <>
struct laplace_options<false> : public laplace_options_base {};

template <>
struct laplace_options<true> : public laplace_options_base {
  /* Value for user supplied initial theta  */
  Eigen::VectorXd theta_0{0};
};


using laplace_options_default = laplace_options<false>;
using laplace_options_theta0 = laplace_options<true>;

seems cleaner to me than require/require_nots and a void

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It looks like this does work, see https://godbolt.org/z/GM4nqM81a

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I like it! Just updated

WardBrian merged commit 94ab922 into laplace/move-theta-to-tols Jun 30, 2025
WardBrian deleted the laplace/compile-time-theta-default branch June 30, 2025 23:49
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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL