Skip to content
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

[RF] RooFit AD refactoring #16772

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions roofit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
set(roofit_legacy_eval_backend ON CACHE BOOL "" FORCE)

add_subdirectory(batchcompute)
add_subdirectory(codegen)
if (roofit_multiprocess)
add_subdirectory(roofitZMQ)
add_subdirectory(multiprocess)
Expand Down
23 changes: 23 additions & 0 deletions roofit/codegen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (C) 1995-2019, Rene Brun and Fons Rademakers.
# All rights reserved.
#
# For the licensing terms see $ROOTSYS/LICENSE.
# For the list of contributors see $ROOTSYS/README/CREDITS.

############################################################################
# CMakeLists.txt file for building ROOT rootfit/histfactory package
# @author Pere Mato, CERN
############################################################################

ROOT_STANDARD_LIBRARY_PACKAGE(RooFitCodegen
HEADERS
RooFit/CodegenImpl.h
SOURCES
src/CodegenImpl.cxx
DICTIONARY_OPTIONS
"-writeEmptyRootPCM"
DEPENDENCIES
RooFit
RooFitCore
HistFactory
)
7 changes: 7 additions & 0 deletions roofit/codegen/LinkDef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifdef __CLING__

#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;

#endif
155 changes: 155 additions & 0 deletions roofit/codegen/inc/RooFit/CodegenImpl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Project: RooFit
* Authors:
* Jonas Rembser, CERN 2024
*
* Copyright (c) 2024, CERN
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted according to the terms
* listed in LICENSE (http://roofit.sourceforge.net/license.txt)
*/

#ifndef RooFit_Detail_CodegenImpl_h
#define RooFit_Detail_CodegenImpl_h

#include <RooFit/CodegenContext.h>

#include <type_traits>

class ParamHistFunc;
class PiecewiseInterpolation;
class RooAbsArg;
class RooAbsReal;
class RooAddPdf;
class RooAddition;
class RooBernstein;
class RooBifurGauss;
class RooCBShape;
class RooChebychev;
class RooConstVar;
class RooConstraintSum;
class RooEffProd;
class RooEfficiency;
class RooExponential;
class RooExtendPdf;
class RooFormulaVar;
class RooGamma;
class RooGaussian;
class RooGenericPdf;
class RooHistFunc;
class RooHistPdf;
class RooLandau;
class RooLognormal;
class RooMultiVarGaussian;
class RooParamHistFunc;
class RooPoisson;
class RooPolyVar;
class RooPolynomial;
class RooProduct;
class RooRatio;
class RooRealIntegral;
class RooRealSumFunc;
class RooRealSumPdf;
class RooRealVar;
class RooRecursiveFraction;
class RooUniform;

namespace RooStats {
namespace HistFactory {
class FlexibleInterpVar;
}
} // namespace RooStats

namespace RooFit {

namespace Detail {
class RooFixedProdPdf;
class RooNLLVarNew;
class RooNormalizedPdf;
} // namespace Detail

class CodegenContext;

void codegenImpl(Detail::RooFixedProdPdf &arg, CodegenContext &ctx);
void codegenImpl(Detail::RooNLLVarNew &arg, CodegenContext &ctx);
void codegenImpl(Detail::RooNormalizedPdf &arg, CodegenContext &ctx);
void codegenImpl(ParamHistFunc &arg, CodegenContext &ctx);
void codegenImpl(PiecewiseInterpolation &arg, CodegenContext &ctx);
void codegenImpl(RooAbsArg &arg, RooFit::CodegenContext &ctx);
void codegenImpl(RooAddPdf &arg, CodegenContext &ctx);
void codegenImpl(RooAddition &arg, CodegenContext &ctx);
void codegenImpl(RooBernstein &arg, CodegenContext &ctx);
void codegenImpl(RooBifurGauss &arg, CodegenContext &ctx);
void codegenImpl(RooCBShape &arg, CodegenContext &ctx);
void codegenImpl(RooChebychev &arg, CodegenContext &ctx);
void codegenImpl(RooConstVar &arg, CodegenContext &ctx);
void codegenImpl(RooConstraintSum &arg, CodegenContext &ctx);
void codegenImpl(RooEffProd &arg, CodegenContext &ctx);
void codegenImpl(RooEfficiency &arg, CodegenContext &ctx);
void codegenImpl(RooExponential &arg, CodegenContext &ctx);
void codegenImpl(RooExtendPdf &arg, CodegenContext &ctx);
void codegenImpl(RooFormulaVar &arg, CodegenContext &ctx);
void codegenImpl(RooGamma &arg, CodegenContext &ctx);
void codegenImpl(RooGaussian &arg, CodegenContext &ctx);
void codegenImpl(RooGenericPdf &arg, CodegenContext &ctx);
void codegenImpl(RooHistFunc &arg, CodegenContext &ctx);
void codegenImpl(RooHistPdf &arg, CodegenContext &ctx);
void codegenImpl(RooLandau &arg, CodegenContext &ctx);
void codegenImpl(RooLognormal &arg, CodegenContext &ctx);
void codegenImpl(RooMultiVarGaussian &arg, CodegenContext &ctx);
void codegenImpl(RooParamHistFunc &arg, CodegenContext &ctx);
void codegenImpl(RooPoisson &arg, CodegenContext &ctx);
void codegenImpl(RooPolyVar &arg, CodegenContext &ctx);
void codegenImpl(RooPolynomial &arg, CodegenContext &ctx);
void codegenImpl(RooProduct &arg, CodegenContext &ctx);
void codegenImpl(RooRatio &arg, CodegenContext &ctx);
void codegenImpl(RooRealIntegral &arg, CodegenContext &ctx);
void codegenImpl(RooRealSumFunc &arg, CodegenContext &ctx);
void codegenImpl(RooRealSumPdf &arg, CodegenContext &ctx);
void codegenImpl(RooRealVar &arg, CodegenContext &ctx);
void codegenImpl(RooRecursiveFraction &arg, CodegenContext &ctx);
void codegenImpl(RooStats::HistFactory::FlexibleInterpVar &arg, CodegenContext &ctx);
void codegenImpl(RooUniform &arg, CodegenContext &ctx);

std::string codegenIntegralImpl(RooAbsReal &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooBernstein &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooBifurGauss &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooCBShape &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooChebychev &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooEfficiency &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooExponential &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooGamma &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooGaussian &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooHistFunc &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooHistPdf &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooLandau &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooLognormal &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooMultiVarGaussian &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooPoisson &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooPolyVar &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooPolynomial &arg, int code, const char *rangeName, CodegenContext &ctx);
std::string codegenIntegralImpl(RooUniform &arg, int code, const char *rangeName, CodegenContext &ctx);

template <class Arg_t, int P>
std::string codegenIntegralImpl(Arg_t &arg, int code, const char *rangeName, CodegenContext &ctx, Prio<P> p)
{
if constexpr (std::is_same<Prio<P>, PrioLowest>::value) {
return codegenIntegralImpl(arg, code, rangeName, ctx);
} else {
return codegenIntegralImpl(arg, code, rangeName, ctx, p.next());
}
}

template <class Arg_t>
struct CodegenIntegralImplCaller {

static auto call(RooAbsReal &arg, int code, const char *rangeName, RooFit::CodegenContext &ctx)
{
return codegenIntegralImpl(static_cast<Arg_t &>(arg), code, rangeName, ctx, PrioHighest{});
}
};
Comment on lines +134 to +151
Copy link
Contributor

Choose a reason for hiding this comment

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

Do I understand correctly that the Caller first tries to call the prio 1 (highest) implementation, and then will loop through all 10 using the Impl template until it finds one that is actually implemented for the Arg_t already so doesn't need to use the template?

Few questions:

  1. Do you actually need all 10 priority levels? How about replacing that with just an overload boolean? It will save some looping through empty functions.
  2. I'm not sure, but there may be some pathological situations. For instance, what if in an interactive session you call some function, the tree of templates gets instantiated and then afterwards you try to define a new overload? Won't that overload then already have been instantiated from the Impl template? Another possible situation would be something similar to the above but in separate compilation units.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great comments!

  1. I was hoping the optimizer gets rid of these loop levels, which are "statically" clear to the interpreter. So in principle they should be optimized out. But it's an interesting question: I'll benchmark how expensive the levels are
  2. You're right, this is a clear limitation, but design: the function pointer that was resolved is cached in an unordered_map, so speed up the lookup for the same class later. I'll check what would happen if the caching is disabled, that's an interesting question!


} // namespace RooFit

#endif
Loading
Loading