Skip to content

Commit

Permalink
[RF] Take out RooFit code generation context outside of Detail namespace
Browse files Browse the repository at this point in the history
We are expecting users to interact with this class, since it is
essential to implement code generation for your own RooFit primitives.

Putting the class in the "Detail" namespace sends the wrong message and
normalized it for users to ignore the convention that "Detail" or
"Internal" stuff should not be used.

While removing the namespace, also rename the class to a shorter and
more succinct name, matching the "codegen" evaluation backend name.
"Codegen" also sounds more friendly than "CodeSquash", which reminds me
of "fly squash".
  • Loading branch information
guitargeek committed Oct 28, 2024
1 parent 4fefb5d commit 94aff8c
Show file tree
Hide file tree
Showing 87 changed files with 176 additions and 185 deletions.
46 changes: 23 additions & 23 deletions roofit/doc/developers/roofit_ad.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ computation, but without any overhead that is hard to digest by the AD tool.
On a high level, this *code generation* is implemented as follows:

1. The computation graph is visited recursively by a
RooFit::Detail::CodeSquashContext object, via the virtual
RooFit::CodegenContext object, via the virtual
RooAbsArg::translate() function that implements the translation of a
given RooFit class to minimal C++ code. This is an example of the visitor
pattern.
Expand Down Expand Up @@ -183,7 +183,7 @@ properties out of the RooFit classes that make up the statistical model.
The `translate()` function helps implement the Code Squashing logic that is
used to optimize numerical evaluations. It accomplishes this by using a small
subset of helper functions that are available in the
`RooFit::Detail::CodeSquashContext` and `RooFuncWrapper` classes
`RooFit::CodegenContext` and `RooFuncWrapper` classes
(see Appendix B). It converts a RooFit expression into a form that can be
efficiently evaluated by Clad.

Expand Down Expand Up @@ -285,7 +285,7 @@ To translate the `RooPoisson` class, create a translate function and in it
include a call to the updated function.

``` {.cpp}
void RooPoisson::translate(RooFit::Detail::RooFit::Detail::CodeSquashContext &ctx) const
void RooPoisson::translate(RooFit::CodegenContext &ctx) const
{
std::string xName = ctx.getResult(x);
if (!_noRounding)
Expand Down Expand Up @@ -326,7 +326,7 @@ more complicated. For a specific class, it will add whatever is represented on
*after* it has AD support.

``` {.cpp}
void RooGaussian::translate(RooFit::Detail::RooFit::Detail::CodeSquashContext &ctx) const
void RooGaussian::translate(RooFit::CodegenContext &ctx) const
{
ctx.addResult(this, ctx.buildCall("RooFit::Detail::MathFuncs::gaussianEvaluate", x, mean, sigma));
}
Expand All @@ -350,7 +350,7 @@ the only way to propagate these upwards into the compute graph.
function can be seen here:

``` {.cpp}
void RooNLLVarNew::translate(RooFit::Detail::RooFit::Detail::CodeSquashContext &ctx) const
void RooNLLVarNew::translate(RooFit::CodegenContext &ctx) const
{
std::string weightSumName = ctx.makeValidVarName(GetName()) + "WeightSum";
std::string resName = ctx.makeValidVarName(GetName()) + "Result";
Expand Down Expand Up @@ -516,7 +516,7 @@ the `res` variable in the following example) of this class, which can then be
propagated in the rest of the compute graph.

``` {.cpp}
void translate(RooFit::Detail::RooFit::Detail::CodeSquashContext &ctx) const override {
void translate(RooFit::CodegenContext &ctx) const override {
std::string res = ctx.buildCall("MathFuncs::doFoo", a, b);
ctx.addResult(this, res);
}
Expand Down Expand Up @@ -561,7 +561,7 @@ return the output using the `buildCall()` function.

``` {.cpp}
std::string
buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::Detail::RooFit::Detail::CodeSquashContext &ctx) const override {
buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::CodegenContext &ctx) const override {
return ctx.buildCall("EvaluateFunc::integralFoo", a, b);
}
```
Expand Down Expand Up @@ -594,13 +594,13 @@ class RooFoo : public RooAbsReal {
}
//// ************************** functions for AD Support ***********************
void translate(RooFit::Detail::RooFit::Detail::CodeSquashContext &ctx) const override {
void translate(RooFit::CodegenContext &ctx) const override {
std::string res = ctx.buildCall("EvaluateFunc::doFoo", a, b);
ctx.addResult(this, res);
}
std::string
buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::Detail::RooFit::Detail::CodeSquashContext &ctx) const override {
buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::CodegenContext &ctx) const override {
return ctx.buildCall("EvaluateFunc::integralFoo", a, b);
}
//// ************************** functions for AD Support ***********************
Expand Down Expand Up @@ -717,19 +717,19 @@ compile times are reasonable, but with an increase in the level of complexity,
Following classes provide several Helper Functions to translate existing logic
into AD-supported logic.

a - RooFit::Detail::CodeSquashContext
a - RooFit::CodegenContext

b - RooFuncWrapper

### a. RooFit::Detail::CodeSquashContext
### a. RooFit::CodegenContext

> [roofit/roofitcore/inc/RooFit/Detail/CodeSquashContext.h](https://github.com/root-project/root/blob/master/roofit/roofitcore/inc/RooFit/Detail/CodeSquashContext.h)
> [roofit/roofitcore/inc/RooFit/CodegenContext.h](https://github.com/root-project/root/blob/master/roofit/roofitcore/inc/RooFit/CodegenContext.h)
It handles how to create a C++ function out of the compute graph (which is
created with different RooFit classes). This C++ function will be independent
of these RooFit classes.

RooFit::Detail::CodeSquashContext helps traverse the compute graph received from RooFit and
RooFit::CodegenContext helps traverse the compute graph received from RooFit and
then it translates that into a single piece of code (a C++ function), that can
then be differentiated using Clad. It also helps evaluate the model.

Expand All @@ -747,7 +747,7 @@ the squashing to happen.

#### Helper Functions

- **RooFit::Detail::CodeSquashContext**: this class maintains the context for squashing of
- **RooFit::CodegenContext**: this class maintains the context for squashing of
RooFit models into code. It keeps track of the results of various
expressions to avoid redundant calculations.

Expand Down Expand Up @@ -787,7 +787,7 @@ code body of the squashed function.
These functions will appear again in this document with more contextual
examples. For detailed in-line documentation (code comments), please see:

> [roofit/roofitcore/src/RooFit/Detail/CodeSquashContext.cxx](https://github.com/root-project/root/blob/master/roofit/roofitcore/src/RooFit/Detail/CodeSquashContext.cxx)
> [roofit/roofitcore/src/RooFit/CodegenContext.cxx](https://github.com/root-project/root/blob/master/roofit/roofitcore/src/RooFit/Detail/CodegenContext.cxx)

### b. RooFuncWrapper
Expand Down Expand Up @@ -828,7 +828,7 @@ examples. For detailed in-line documentation (code comments), please see:

## Appendix C - Helper functions discussed in this document

- **RooFit::Detail::CodeSquashContext::addResult()**: For a specific class, it
- **RooFit::CodegenContext::addResult()**: For a specific class, it
will add whatever is represented on the right-hand side (a function call, an
expression, etc.) to the result of this class, which can then be propagated in
the rest of the compute graph. A to call `addResult()`must be included in
Expand All @@ -838,51 +838,51 @@ expression, etc.) to the result of this class, which can then be propagated in
new name to assign/overwrite).
- Output: Adds (or overwrites) the string representing the result of a node.

- **RooFit::Detail::CodeSquashContext::getResult()**: It helps lookup the
- **RooFit::CodegenContext::getResult()**: It helps lookup the
result of a child node (the string that the child node previously saved in a
variable using the `addResult()` function).

- Input: `key` (the node to get the result string for).
- Output: String representing the result of this node.

- **RooFit::Detail::CodeSquashContext::addToCodeBody()**: Takes whatever string
- **RooFit::CodegenContext::addToCodeBody()**: Takes whatever string
is computed in its arguments and adds it to the overall function string (which
will later be just-in-time compiled).

- Inputs: `klass` (the class requesting this addition, usually 'this'), `in`
(string to add to the squashed code).
- Output: Adds the input string to the squashed code body.

- **RooFit::Detail::CodeSquashContext::addToGlobalScope()**: Helps declare and
- **RooFit::CodegenContext::addToGlobalScope()**: Helps declare and
initialize the results variable, so that it can be available globally
(throughout the function body).

- Input: `str` (the string to add to the global scope).
- Output: Adds the given string to the string block that will be emitted at
the top of the squashed function.

- **RooFit::Detail::CodeSquashContext::assembleCode()**: combines the generated
- **RooFit::CodegenContext::assembleCode()**: combines the generated
code statements into the final code body of the squashed function.

- Input: `returnExpr` (he string representation of what the squashed function
should return, usually the head node).
- Output: The final body of the function.

- **RooFit::Detail::CodeSquashContext::beginLoop()**: The code squashing task
- **RooFit::CodegenContext::beginLoop()**: The code squashing task
will automatically build a For loop around the indented statements that follow
this function.

- Input: `in` (a pointer to the calling class, used to determine the loop
dependent variables).
- Output: A scope for iterating over vector observables.

- **RooFit::Detail::CodeSquashContext::buildArg()**: helps convert RooFit
- **RooFit::CodegenContext::buildArg()**: helps convert RooFit
objects into arrays or other C++ representations for efficient computation.

- Input: `in` (the list to convert to array).
- Output: Name of the array that stores the input list in the squashed code.

- **RooFit::Detail::CodeSquashContext::buildCall()**: Creates a string
- **RooFit::CodegenContext::buildCall()**: Creates a string
representation of the function to be called and its arguments.

- Input: A function with name `funcname`, passing some arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace HistFactory{

void doEval(RooFit::EvalContext &) const override;

void translate(RooFit::Detail::CodeSquashContext &ctx) const override;
void translate(RooFit::CodegenContext &ctx) const override;

protected:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class ParamHistFunc : public RooAbsReal {
double evaluate() const override;
void doEval(RooFit::EvalContext &) const override;

void translate(RooFit::Detail::CodeSquashContext &ctx) const override;
void translate(RooFit::CodegenContext &ctx) const override;

private:
static NumBins getNumBinsPerDim(RooArgSet const& vars);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class PiecewiseInterpolation : public RooAbsReal {
std::list<double>* plotSamplingHint(RooAbsRealLValue& obs, double xlo, double xhi) const override ;
bool isBinnedDistribution(const RooArgSet& obs) const override ;

void translate(RooFit::Detail::CodeSquashContext &ctx) const override;
void translate(RooFit::CodegenContext &ctx) const override;

protected:

Expand Down
2 changes: 1 addition & 1 deletion roofit/histfactory/src/FlexibleInterpVar.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ double FlexibleInterpVar::evaluate() const
return total;
}

void FlexibleInterpVar::translate(RooFit::Detail::CodeSquashContext &ctx) const
void FlexibleInterpVar::translate(RooFit::CodegenContext &ctx) const
{
unsigned int n = _interpCode.size();

Expand Down
2 changes: 1 addition & 1 deletion roofit/histfactory/src/ParamHistFunc.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ double ParamHistFunc::evaluate() const
return getParameter().getVal();
}

void ParamHistFunc::translate(RooFit::Detail::CodeSquashContext &ctx) const
void ParamHistFunc::translate(RooFit::CodegenContext &ctx) const
{
auto const &n = _numBinsPerDim;

Expand Down
2 changes: 1 addition & 1 deletion roofit/histfactory/src/PiecewiseInterpolation.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ double PiecewiseInterpolation::evaluate() const

}

void PiecewiseInterpolation::translate(RooFit::Detail::CodeSquashContext &ctx) const
void PiecewiseInterpolation::translate(RooFit::CodegenContext &ctx) const
{
std::size_t n = _interpCode.size();

Expand Down
4 changes: 2 additions & 2 deletions roofit/roofit/inc/RooBernstein.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class RooBernstein : public RooAbsPdf {
double analyticalIntegral(Int_t code, const char *rangeName = nullptr) const override;
void selectNormalizationRange(const char *rangeName = nullptr, bool force = false) override;

void translate(RooFit::Detail::CodeSquashContext &ctx) const override;
void translate(RooFit::CodegenContext &ctx) const override;
std::string buildCallToAnalyticIntegral(Int_t code, const char *rangeName,
RooFit::Detail::CodeSquashContext &ctx) const override;
RooFit::CodegenContext &ctx) const override;

private:
void fillBuffer() const;
Expand Down
4 changes: 2 additions & 2 deletions roofit/roofit/inc/RooBifurGauss.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class RooBifurGauss : public RooAbsPdf {
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName = nullptr) const override;
double analyticalIntegral(Int_t code, const char *rangeName = nullptr) const override;

void translate(RooFit::Detail::CodeSquashContext &ctx) const override;
void translate(RooFit::CodegenContext &ctx) const override;
std::string buildCallToAnalyticIntegral(Int_t code, const char *rangeName,
RooFit::Detail::CodeSquashContext &ctx) const override;
RooFit::CodegenContext &ctx) const override;

protected:
RooRealProxy x;
Expand Down
4 changes: 2 additions & 2 deletions roofit/roofit/inc/RooCBShape.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class RooCBShape : public RooAbsPdf {
Int_t getMaxVal(const RooArgSet& vars) const override ;
double maxVal(Int_t code) const override ;

void translate(RooFit::Detail::CodeSquashContext &ctx) const override;
void translate(RooFit::CodegenContext &ctx) const override;
std::string
buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::Detail::CodeSquashContext &ctx) const override;
buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::CodegenContext &ctx) const override;

protected:

Expand Down
4 changes: 2 additions & 2 deletions roofit/roofit/inc/RooChebychev.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class RooChebychev : public RooAbsPdf {

void selectNormalizationRange(const char* rangeName=nullptr, bool force=false) override ;

void translate(RooFit::Detail::CodeSquashContext &ctx) const override;
void translate(RooFit::CodegenContext &ctx) const override;
std::string
buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::Detail::CodeSquashContext &ctx) const override;
buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::CodegenContext &ctx) const override;

private:
RooRealProxy _x;
Expand Down
4 changes: 2 additions & 2 deletions roofit/roofit/inc/RooExponential.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class RooExponential : public RooAbsPdf {

bool negateCoefficient() const { return _negateCoefficient; }

void translate(RooFit::Detail::CodeSquashContext &ctx) const override;
void translate(RooFit::CodegenContext &ctx) const override;
std::string buildCallToAnalyticIntegral(Int_t code, const char *rangeName,
RooFit::Detail::CodeSquashContext &ctx) const override;
RooFit::CodegenContext &ctx) const override;

protected:
RooRealProxy x;
Expand Down
4 changes: 2 additions & 2 deletions roofit/roofit/inc/RooGamma.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class RooGamma : public RooAbsPdf {
Int_t getGenerator(const RooArgSet& directVars, RooArgSet &generateVars, bool staticInitOK=true) const override;
void generateEvent(Int_t code) override;

void translate(RooFit::Detail::CodeSquashContext &ctx) const override;
void translate(RooFit::CodegenContext &ctx) const override;
std::string
buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::Detail::CodeSquashContext &ctx) const override;
buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::CodegenContext &ctx) const override;

protected:

Expand Down
4 changes: 2 additions & 2 deletions roofit/roofit/inc/RooGaussian.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class RooGaussian : public RooAbsPdf {
/// Get the sigma parameter.
RooAbsReal const& getSigma() const { return sigma.arg(); }

void translate(RooFit::Detail::CodeSquashContext &ctx) const override;
void translate(RooFit::CodegenContext &ctx) const override;
std::string
buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::Detail::CodeSquashContext &ctx) const override;
buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::CodegenContext &ctx) const override;

protected:

Expand Down
4 changes: 2 additions & 2 deletions roofit/roofit/inc/RooLandau.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class RooLandau : public RooAbsPdf {
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName = nullptr) const override;
double analyticalIntegral(Int_t code, const char *rangeName) const override;

void translate(RooFit::Detail::CodeSquashContext &ctx) const override;
void translate(RooFit::CodegenContext &ctx) const override;
std::string
buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::Detail::CodeSquashContext &ctx) const override;
buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::CodegenContext &ctx) const override;

protected:

Expand Down
4 changes: 2 additions & 2 deletions roofit/roofit/inc/RooLognormal.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class RooLognormal : public RooAbsPdf {
Int_t getGenerator(const RooArgSet &directVars, RooArgSet &generateVars, bool staticInitOK = true) const override;
void generateEvent(Int_t code) override;

void translate(RooFit::Detail::CodeSquashContext &ctx) const override;
void translate(RooFit::CodegenContext &ctx) const override;
std::string
buildCallToAnalyticIntegral(int code, const char *rangeName, RooFit::Detail::CodeSquashContext &ctx) const override;
buildCallToAnalyticIntegral(int code, const char *rangeName, RooFit::CodegenContext &ctx) const override;

/// Get the x variable.
RooAbsReal const &getX() const { return x.arg(); }
Expand Down
2 changes: 1 addition & 1 deletion roofit/roofit/inc/RooParamHistFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class RooParamHistFunc : public RooAbsReal {

const RooArgList& paramList() const { return _p ; }

void translate(RooFit::Detail::CodeSquashContext &ctx) const override;
void translate(RooFit::CodegenContext &ctx) const override;

protected:

Expand Down
4 changes: 2 additions & 2 deletions roofit/roofit/inc/RooPoisson.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class RooPoisson : public RooAbsPdf {
/// Get the mean parameter.
RooAbsReal const& getMean() const { return mean.arg(); }

void translate(RooFit::Detail::CodeSquashContext &ctx) const override;
void translate(RooFit::CodegenContext &ctx) const override;
std::string
buildCallToAnalyticIntegral(int code, const char *rangeName, RooFit::Detail::CodeSquashContext &ctx) const override;
buildCallToAnalyticIntegral(int code, const char *rangeName, RooFit::CodegenContext &ctx) const override;

protected:

Expand Down
4 changes: 2 additions & 2 deletions roofit/roofit/inc/RooPolynomial.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ class RooPolynomial : public RooAbsPdf {
// pdf is a reducer node because it doesn't depend on the observables.
bool isReducerNode() const override { return _coefList.empty(); }

void translate(RooFit::Detail::CodeSquashContext &ctx) const override;
void translate(RooFit::CodegenContext &ctx) const override;
std::string buildCallToAnalyticIntegral(Int_t code, const char *rangeName,
RooFit::Detail::CodeSquashContext &ctx) const override;
RooFit::CodegenContext &ctx) const override;

protected:
RooRealProxy _x;
Expand Down
4 changes: 2 additions & 2 deletions roofit/roofit/inc/RooUniform.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class RooUniform : public RooAbsPdf {
Int_t getGenerator(const RooArgSet& directVars, RooArgSet &generateVars, bool staticInitOK=true) const override;
void generateEvent(Int_t code) override;

void translate(RooFit::Detail::CodeSquashContext &ctx) const override;
void translate(RooFit::CodegenContext &ctx) const override;
std::string
buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::Detail::CodeSquashContext &ctx) const override;
buildCallToAnalyticIntegral(Int_t code, const char *rangeName, RooFit::CodegenContext &ctx) const override;

protected:

Expand Down
Loading

0 comments on commit 94aff8c

Please sign in to comment.