Repository navigation
[6.42][RF] Evaluate logarithms of pdfs and products directly in likelihoods - #24282
Merged
Merged
Conversation
Large likelihood fits (e.g. in CMS and ATLAS) often contain many Gaussian constraint terms. In the standard evaluation, these incur unnecessary overhead by computing the PDF value via `exp(...)` only to immediately take the logarithm in the constraint term. This commit introduces a log-space optimization that avoids the redundant exp -> log roundtrip. A class that can output its logarithm overrides the new RooAbsArg::canOutputLogarithm(). The Evaluator decides for which nodes this is used, and doEval() reads the decision from the new EvalContext::outputsLogarithm(). Evaluator::print() shows it in a new column, because the value printed for such a node is a logarithm. We need this optimization because it makes a difference for CMS, and they have also implemented something like this in the custom CMS Combine likelihood classes. If we want them to gradually move to "vanilla" RooFit, we need at least the same optimizations. Assisted-by: claude-code:claude-fable-5-2 (cherry picked from commit 24ac321)
Generalize the logarithm optimization from the constraint terms to all nodes whose only value client takes the logarithm anyway, which are the RooConstraintSum and the unbinned RooNLLVarNew. A RooFixedProdPdf or RooNormalizedPdf that outputs its logarithm requests the same from its servers. A class tells with the new RooAbsArg::canUseLogarithmOf() from which servers it accepts logarithms. Evaluator::setupLogEvaluation() then requests the logarithm from a server that has no other client and whose values are not given as input. With the cpu backend, the likelihood of a product with many factors is then finite even if the product underflows. The factors that can't provide their logarithm are multiplied, and if a partial product left the range of normal doubles, the product is recomputed with its binary exponent kept separate. Avoiding exp() and log() speeds up Gaussian fits with 1M events by 2.6x for one Gaussian and 2.2x for a product of two. Products of factors without logarithms get about 2% slower. The logarithms are only requested for nodes evaluated on the CPU, so the cuda backend can still underflow. With the codegen backend, only the likelihood value benefits, not the gradient. The RooRealIntegral doesn't output its logarithm anymore, because a negative integral then lost the badness packed into the NaN. The tests that compared the cpu backend exactly with the RooAbsL classes now use tolerances: the likelihoods differ by 1 ULP, the fitted parameters by up to 1e-10. This fixes the reproducer of root-project#24202 with the cpu backend. Assisted-by: claude-code:claude-fable-5-2 (cherry picked from commit 6bf1cd0)
The codegen backend wrapped the code of every node in "// Begin -- ..." and "// End -- ..." comments with the printed node. For big fits, the node names are long and obfuscated, so the comments made the code harder to read, not easier. Printing a node also evaluates it in scalar mode. Creating a likelihood could therefore report evaluation errors, e.g. for a product that underflows. The comments also ended the line of every piece of code. Now, the CodegenContext does that itself, such that a loop pragma can't end up behind other code on the same line. Assisted-by: claude-code:claude-fable-5-2 (cherry picked from commit 9a5ef56)
Like the cpu backend, the code generated for the unbinned likelihood and the constraint sum now uses the logarithms of the pdf values directly. For that, the CodegenContext gets getLogResult(), which dispatches to codegenLogImpl() overloads and falls back to std::log() of the value. Products sum the logarithms of their factors, and Gaussians avoid exp(). The now unused MathFuncs::constraintSum() and the unbinned branch of MathFuncs::nll() are removed. The sums are generated with one statement per term. The compiler and Clad recurse for every operator of an expression, so a single expression with several thousand terms would overflow the stack. Reproducing that takes minutes, so the test checks the number of operators per generated statement instead. The likelihood of a product with many factors is then finite with codegen too. The gradients also get 1.7x faster for products of non-Gaussian pdfs and up to 10x for Gaussians. Without the gradient, a product of three non-Gaussian pdfs gets 7% slower, because of the additional logarithms. Assisted-by: claude-code:claude-fable-5-2 (cherry picked from commit e16b6ad)
With the cuda backend, the likelihood of a product with many factors was still infinite, because only nodes evaluated on the CPU output their logarithms. The Evaluator now sets the mode in both evaluation contexts, independent of the device. RooNormalizedPdf computes its logarithm with a new LogNormalizedPdf compute function instead of a host loop, so that it also works on the GPU. It first processes all events in a loop without branches and then recomputes the ones with evaluation errors or zero pdf values, which makes fits of one Gaussian with 1M events 10% faster than with the host loop. The other compute functions used in log mode already supported CUDA. The tests of products with many factors now check the likelihood with the cpu, cuda and codegen_no_grad backends, and the codegen gradient by comparing a Minuit 2 fit with strategy 0 to a fit with numeric derivatives. The data is chosen such that the minimum is within the parameter limits and the product underflows there. Assisted-by: claude-code:claude-fable-5-2 (cherry picked from commit c7e67b3)
On the 6.42 branch, the test also runs with the legacy backend, which doesn't exist on master anymore. Creating a legacy likelihood resets the evaluation error logging mode to PrintErrors, so the errors were printed instead of collected and the test counted none. Set the logging mode after creating the likelihood, and expect the additional error that the legacy likelihood logs for its own NaN value. Assisted-by: claude-code:claude-opus-5-5
Test Results 24 files 24 suites 3d 22h 34m 3s ⏱️ For more details on these failures, see this check. Results for commit 30fb32c. ♻️ This comment has been updated with latest results. |
dpiparo
merged commit Nov 21, 2026
b52fb1d
into
root-project:v6-42-00-patches
59 of 63 checks passed
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.
Backport of #21325, required for the ATLAS Higgs discovery model publication.