Skip to content

Commit

Permalink
[RF] Adapt rootbench to new RooFit::EvalBackend() command argument
Browse files Browse the repository at this point in the history
  • Loading branch information
guitargeek committed Sep 22, 2023
1 parent 3fc52b7 commit 09841fb
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 72 deletions.
16 changes: 10 additions & 6 deletions root/roofit/histfactory/benchHistFactory.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TestData {
auto *pdf = w->pdf("simPdf");

std::unique_ptr<RooAbsReal> nll{pdf->createNLL(*w->data("obsData"), Constrain(constraintParams),
GlobalObservables(*mc->GetGlobalObservables()), BatchMode(false))};
GlobalObservables(*mc->GetGlobalObservables()), EvalBackend::Legacy())};

RooMinimizer m(*nll);
m.setPrintLevel(-1);
Expand Down Expand Up @@ -70,9 +70,10 @@ static void benchHistFactory001(benchmark::State &state)

auto *pdf = w->pdf("simPdf");

auto evalBackend = static_cast<EvalBackend::Value>(state.range(1));
std::unique_ptr<RooAbsReal> nll{pdf->createNLL(*w->data("obsData"), Constrain(g_testData.constraintParams),
GlobalObservables(*mc->GetGlobalObservables()),
BatchMode(state.range(1)))};
EvalBackend(evalBackend))};

auto &minimizationPath = *g_testData.minimizationPath;

Expand All @@ -95,9 +96,12 @@ static void benchHistFactory001(benchmark::State &state)

auto const unit = benchmark::kMillisecond;

BENCHMARK(benchHistFactory001)->Unit(unit)->Args({1, 0})->Name("hf001__BinnedFitOptimization_ON___BatchMode_OFF");
BENCHMARK(benchHistFactory001)->Unit(unit)->Args({0, 0})->Name("hf001__BinnedFitOptimization_OFF__BatchMode_OFF");
BENCHMARK(benchHistFactory001)->Unit(unit)->Args({1, 1})->Name("hf001__BinnedFitOptimization_ON___BatchMode_ON_");
BENCHMARK(benchHistFactory001)->Unit(unit)->Args({0, 1})->Name("hf001__BinnedFitOptimization_OFF__BatchMode_ON_");
auto Legacy = static_cast<int>(RooFit::EvalBackend::Value::Legacy);
auto Cpu = static_cast<int>(RooFit::EvalBackend::Value::Cpu);

BENCHMARK(benchHistFactory001)->Unit(unit)->Args({1, Legacy})->Name("hf001__BinnedFitOptimization_ON___BatchMode_OFF");
BENCHMARK(benchHistFactory001)->Unit(unit)->Args({0, Legacy})->Name("hf001__BinnedFitOptimization_OFF__BatchMode_OFF");
BENCHMARK(benchHistFactory001)->Unit(unit)->Args({1, Cpu})->Name("hf001__BinnedFitOptimization_ON___BatchMode_ON_");
BENCHMARK(benchHistFactory001)->Unit(unit)->Args({0, Cpu})->Name("hf001__BinnedFitOptimization_OFF__BatchMode_ON_");

BENCHMARK_MAIN();
8 changes: 4 additions & 4 deletions root/roofit/roofit/RooFitBinnedBenchmarks.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace {
const int nChannelsForBinScan = 1;
const std::vector<int> nCPUVector {1, 2, 3};

constexpr bool batchMode = true;
constexpr auto evalBackend = RooFit::EvalBackend::Value::Cpu;

auto const timeUnit = benchmark::kMillisecond;

Expand Down Expand Up @@ -154,7 +154,7 @@ static void BM_RooFit_BinnedTestMigrad(benchmark::State &state)
std::unique_ptr<RooAbsData> data{w->data("obsData")};
std::unique_ptr<ModelConfig> mc{static_cast<ModelConfig *>(w->genobj("ModelConfig"))};
std::unique_ptr<RooAbsPdf> pdf{w->pdf(mc->GetPdf()->GetName())};
std::unique_ptr<RooAbsReal> nll{pdf->createNLL(*data, NumCPU(cpu, 0), BatchMode(batchMode))};
std::unique_ptr<RooAbsReal> nll{pdf->createNLL(*data, NumCPU(cpu, 0), EvalBackend(evalBackend))};
RooMinimizer m(*nll);
m.setPrintLevel(-1);
m.setStrategy(0);
Expand Down Expand Up @@ -183,7 +183,7 @@ static void BM_RooFit_BinnedTestHesse(benchmark::State &state)
RooAbsData *data = w->data("obsData");
ModelConfig *mc = static_cast<ModelConfig *>(w->genobj("ModelConfig"));
std::unique_ptr<RooAbsPdf> pdf{w->pdf(mc->GetPdf()->GetName())};
std::unique_ptr<RooAbsReal> nll{pdf->createNLL(*data, NumCPU(cpu, 0), BatchMode(batchMode))};
std::unique_ptr<RooAbsReal> nll{pdf->createNLL(*data, NumCPU(cpu, 0), EvalBackend(evalBackend))};
RooArgSet params;
pdf->getParameters(data->get(), params);
w->saveSnapshot("no_fit", params, true);
Expand Down Expand Up @@ -220,7 +220,7 @@ static void BM_RooFit_BinnedTestMinos(benchmark::State &state)
std::unique_ptr<RooAbsData> data{w->data("obsData")};
std::unique_ptr<ModelConfig> mc{static_cast<ModelConfig *>(w->genobj("ModelConfig"))};
std::unique_ptr<RooAbsPdf> pdf{w->pdf(mc->GetPdf()->GetName())};
std::unique_ptr<RooAbsReal> nll{pdf->createNLL(*data, NumCPU(cpu, 0), BatchMode(batchMode))};
std::unique_ptr<RooAbsReal> nll{pdf->createNLL(*data, NumCPU(cpu, 0), EvalBackend(evalBackend))};
RooArgSet params;
pdf->getParameters(data->get(), params);
w->saveSnapshot("no_fit", params, true);
Expand Down
84 changes: 38 additions & 46 deletions root/roofit/roofit/RooFitUnBinnedBenchmarks.cxx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#include <RooGlobalFunc.h>
#include <RooHelpers.h>
#include <RooMsgService.h>
#include <RooAddModel.h>
#include <RooRealVar.h>
#include <RooDataSet.h>
#include <RooGaussian.h>
#include <RooGaussModel.h>
#include <RooCategory.h>
#include <RooBMixDecay.h>
#include <RooBCPEffDecay.h>
#include <RooBDecay.h>
#include <RooBMixDecay.h>
#include <RooCategory.h>
#include <RooDataSet.h>
#include <RooDecay.h>
#include <RooFormulaVar.h>
#include <RooTruthModel.h>
#include <RooGaussModel.h>
#include <RooGaussian.h>
#include <RooGlobalFunc.h>
#include <RooHelpers.h>
#include <RooMinimizer.h>
#include <RooMsgService.h>
#include <RooRealVar.h>
#include <RooTruthModel.h>

#include <benchmark/benchmark.h>

Expand All @@ -33,15 +33,6 @@ void shiftParameters(const RooArgSet &parameters)
}
}

std::string convertBatchModeToString(int batchMode)
{
if (batchMode == 1)
return "cpu";
if (batchMode == 2)
return "cuda";
return "off";
}

std::unique_ptr<RooHelpers::LocalChangeMsgLevel> makeChangeMsgLvl()
{
auto &msg = RooMsgService::instance();
Expand All @@ -56,15 +47,15 @@ std::unique_ptr<RooHelpers::LocalChangeMsgLevel> makeChangeMsgLvl()

} // namespace

static void BM_RooFit_BDecayWithMixing(benchmark::State &state)
static void BDecayWithMixing(benchmark::State &state)
{
using namespace RooFit;

auto changeMsgLevel = makeChangeMsgLvl();

gErrorIgnoreLevel = kInfo;
int events = state.range(0);
int batchMode = state.range(1);
auto evalBackend = static_cast<RooFit::EvalBackend::Value>(state.range(1));
int cpu = state.range(2);

RooRealVar dt("dt", "dt", -10, 10);
Expand Down Expand Up @@ -98,8 +89,7 @@ static void BM_RooFit_BDecayWithMixing(benchmark::State &state)
params.snapshot(paramsInitial);

// Create NLL
auto bm = convertBatchModeToString(batchMode);
std::unique_ptr<RooAbsReal> nll{bmix.createNLL(*data, NumCPU(cpu, 0), BatchMode(bm))};
std::unique_ptr<RooAbsReal> nll{bmix.createNLL(*data, NumCPU(cpu, 0), EvalBackend(evalBackend))};
RooMinimizer m(*nll);
m.setPrintLevel(-1);
m.setStrategy(0);
Expand All @@ -111,15 +101,15 @@ static void BM_RooFit_BDecayWithMixing(benchmark::State &state)
}
}

static void BM_RooFit_BDecayGaussResolution(benchmark::State &state)
static void BDecayGaussResolution(benchmark::State &state)
{
using namespace RooFit;

auto changeMsgLevel = makeChangeMsgLvl();

gErrorIgnoreLevel = kInfo;
int events = state.range(0);
int batchMode = state.range(1);
auto evalBackend = static_cast<RooFit::EvalBackend::Value>(state.range(1));
int cpu = state.range(2);

RooRealVar dt("dt", "dt", -10, 10);
Expand Down Expand Up @@ -161,8 +151,7 @@ static void BM_RooFit_BDecayGaussResolution(benchmark::State &state)
params.snapshot(paramsInitial);

// Create NLL
auto bm = convertBatchModeToString(batchMode);
std::unique_ptr<RooAbsReal> nll{decay_gm1.createNLL(*data, NumCPU(cpu, 0), BatchMode(bm))};
std::unique_ptr<RooAbsReal> nll{decay_gm1.createNLL(*data, NumCPU(cpu, 0), EvalBackend(evalBackend))};
RooMinimizer m(*nll);
m.setPrintLevel(-1);
m.setStrategy(0);
Expand All @@ -173,15 +162,15 @@ static void BM_RooFit_BDecayGaussResolution(benchmark::State &state)
}
}

static void BM_RooFit_BDecayDoubleGauss(benchmark::State &state)
static void BDecayDoubleGauss(benchmark::State &state)
{
using namespace RooFit;

RooHelpers::LocalChangeMsgLevel changeMsgLvl(RooFit::WARNING);

gErrorIgnoreLevel = kInfo;
int events = state.range(0);
int batchMode = state.range(1);
auto evalBackend = static_cast<RooFit::EvalBackend::Value>(state.range(1));
int cpu = state.range(2);

RooRealVar dt("dt", "dt", -10, 10);
Expand Down Expand Up @@ -233,8 +222,7 @@ static void BM_RooFit_BDecayDoubleGauss(benchmark::State &state)
params.snapshot(paramsInitial);

// Create NLL
auto bm = convertBatchModeToString(batchMode);
std::unique_ptr<RooAbsReal> nll{decay_gmsum.createNLL(*data, NumCPU(cpu, 0), BatchMode(bm))};
std::unique_ptr<RooAbsReal> nll{decay_gmsum.createNLL(*data, NumCPU(cpu, 0), EvalBackend(evalBackend))};
RooMinimizer m(*nll);
m.setPrintLevel(-1);
m.setStrategy(0);
Expand All @@ -248,30 +236,34 @@ static void BM_RooFit_BDecayDoubleGauss(benchmark::State &state)
int nEvents = 100000;
const auto unit = benchmark::kMillisecond;

auto Legacy = static_cast<int>(RooFit::EvalBackend::Value::Legacy);
auto Cpu = static_cast<int>(RooFit::EvalBackend::Value::Cpu);
auto Cuda = static_cast<int>(RooFit::EvalBackend::Value::Cuda);

#define ARGS UseRealTime()->Unit(unit)

BENCHMARK(BM_RooFit_BDecayWithMixing)->Name("BDecayWithMixing_FitLegacy")->Args({nEvents, 0, 1})->ARGS;
BENCHMARK(BM_RooFit_BDecayWithMixing)->Name("BDecayWithMixing_FitLegacyNumCPU2")->Args({nEvents, 0, 2})->ARGS;
BENCHMARK(BM_RooFit_BDecayWithMixing)->Name("BDecayWithMixing_FitLegacyNumCPU4")->Args({nEvents, 0, 4})->ARGS;
BENCHMARK(BM_RooFit_BDecayWithMixing)->Name("BDecayWithMixing_FitCPU")->Args({nEvents, 1, 1})->ARGS;
BENCHMARK(BDecayWithMixing)->Name("BDecayWithMixing_FitLegacy")->Args({nEvents, Legacy, 1})->ARGS;
BENCHMARK(BDecayWithMixing)->Name("BDecayWithMixing_FitLegacyNumCPU2")->Args({nEvents, Legacy, 2})->ARGS;
BENCHMARK(BDecayWithMixing)->Name("BDecayWithMixing_FitLegacyNumCPU4")->Args({nEvents, Legacy, 4})->ARGS;
BENCHMARK(BDecayWithMixing)->Name("BDecayWithMixing_FitCPU")->Args({nEvents, Cpu, 1})->ARGS;
#ifdef R__HAS_CUDA
BENCHMARK(BM_RooFit_BDecayWithMixing)->Name("BDecayWithMixing_FitCUDA")->Args({nEvents, 2, 1})->ARGS;
BENCHMARK(BDecayWithMixing)->Name("BDecayWithMixing_FitCUDA")->Args({nEvents, Cuda, 1})->ARGS;
#endif

BENCHMARK(BM_RooFit_BDecayGaussResolution)->Name("BDecayGaussResolution_FitLegacy")->Args({nEvents, 0, 1})->ARGS;
BENCHMARK(BM_RooFit_BDecayGaussResolution)->Name("BDecayGaussResolution_FitLegacyNumCPU2")->Args({nEvents, 0, 2})->ARGS;
BENCHMARK(BM_RooFit_BDecayGaussResolution)->Name("BDecayGaussResolution_FitLegacyNumCPU4")->Args({nEvents, 0, 4})->ARGS;
BENCHMARK(BM_RooFit_BDecayGaussResolution)->Name("BDecayGaussResolution_FitCPU")->Args({nEvents, 1, 1})->ARGS;
BENCHMARK(BDecayGaussResolution)->Name("BDecayGaussResolution_FitLegacy")->Args({nEvents, Legacy, 1})->ARGS;
BENCHMARK(BDecayGaussResolution)->Name("BDecayGaussResolution_FitLegacyNumCPU2")->Args({nEvents, Legacy, 2})->ARGS;
BENCHMARK(BDecayGaussResolution)->Name("BDecayGaussResolution_FitLegacyNumCPU4")->Args({nEvents, Legacy, 4})->ARGS;
BENCHMARK(BDecayGaussResolution)->Name("BDecayGaussResolution_FitCPU")->Args({nEvents, Cpu, 1})->ARGS;
#ifdef R__HAS_CUDA
BENCHMARK(BM_RooFit_BDecayGaussResolution)->Name("BDecayGaussResolution_FitCUDA")->Args({nEvents, 2, 1})->ARGS;
BENCHMARK(BDecayGaussResolution)->Name("BDecayGaussResolution_FitCUDA")->Args({nEvents, Cuda, 1})->ARGS;
#endif

BENCHMARK(BM_RooFit_BDecayDoubleGauss)->Name("BDecayDoubleGauss_FitLegacy")->Args({nEvents, 0, 1})->ARGS;
BENCHMARK(BM_RooFit_BDecayDoubleGauss)->Name("BDecayDoubleGauss_FitLegacyNumCPU2")->Args({nEvents, 0, 2})->ARGS;
BENCHMARK(BM_RooFit_BDecayDoubleGauss)->Name("BDecayDoubleGauss_FitLegacyNumCPU4")->Args({nEvents, 0, 4})->ARGS;
BENCHMARK(BM_RooFit_BDecayDoubleGauss)->Name("BDecayDoubleGauss_FitCPU")->Args({nEvents, 1, 1})->ARGS;
BENCHMARK(BDecayDoubleGauss)->Name("BDecayDoubleGauss_FitLegacy")->Args({nEvents, Legacy, 1})->ARGS;
BENCHMARK(BDecayDoubleGauss)->Name("BDecayDoubleGauss_FitLegacyNumCPU2")->Args({nEvents, Legacy, 2})->ARGS;
BENCHMARK(BDecayDoubleGauss)->Name("BDecayDoubleGauss_FitLegacyNumCPU4")->Args({nEvents, Legacy, 4})->ARGS;
BENCHMARK(BDecayDoubleGauss)->Name("BDecayDoubleGauss_FitCPU")->Args({nEvents, Cpu, 1})->ARGS;
#ifdef R__HAS_CUDA
BENCHMARK(BM_RooFit_BDecayDoubleGauss)->Name("BDecayDoubleGauss_FitCUDA")->Args({nEvents, 2, 1})->ARGS;
BENCHMARK(BDecayDoubleGauss)->Name("BDecayDoubleGauss_FitCUDA")->Args({nEvents, Cuda, 1})->ARGS;
#endif

#undef ARGS
Expand Down
26 changes: 13 additions & 13 deletions root/roofit/roofit/benchCodeSquashAD.cxx
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
#include <RooAbsData.h>
#include <RooAbsPdf.h>
#include <RooAddition.h>
#include <RooAddPdf.h>
#include <RooAddition.h>
#include <RooCategory.h>
#include <RooChebychev.h>
#include <RooConstVar.h>
#include <RooDataSet.h>
#include <RooDataHist.h>
#include <RooDataSet.h>
#include <RooExponential.h>
#include <RooFitResult.h>
#include <RooGaussian.h>
#include <RooMinimizer.h>
#include <RooProduct.h>
#include <RooRandom.h>
#include <RooRealVar.h>
#include <RooFitResult.h>
#include <RooCategory.h>
#include <RooSimultaneous.h>

#include <TROOT.h>
#include <TSystem.h>
#include <TMath.h>
#include <Math/Factory.h>
#include <Math/Minimizer.h>

#include <TMath.h>
#include <TROOT.h>
#include <TSystem.h>

#include "BenchmarkUtils.h"

#include "benchmark/benchmark.h"
Expand Down Expand Up @@ -115,19 +116,18 @@ static void BM_RooFuncWrapper_ManyParams_Minimization(benchmark::State &state)
RooArgSet origParams;
params.snapshot(origParams);

std::unique_ptr<RooAbsReal> nllRef{model.createNLL(data, BatchMode("off"), Offset("off"))};
std::unique_ptr<RooAbsReal> nllRefBatch{model.createNLL(data, BatchMode("cpu"), Offset("off"))};
std::unique_ptr<RooAbsReal> nllFunc{model.createNLL(data, BatchMode("codegen"), Offset("off"))};
std::unique_ptr<RooAbsReal> nllRef{model.createNLL(data, EvalBackend::Legacy(), Offset("off"))};
std::unique_ptr<RooAbsReal> nllRefBatch{model.createNLL(data, EvalBackend::Cpu(), Offset("off"))};
std::unique_ptr<RooAbsReal> nllFunc{model.createNLL(data, EvalBackend::Codegen(), Offset("off"))};
std::unique_ptr<RooAbsReal> nllFuncNoGrad{model.createNLL(data, EvalBackend::CodegenNoGrad(), Offset("off"))};

std::unique_ptr<RooMinimizer> m = nullptr;

int code = state.range(0);
if (code == RooFitADBenchmarksUtils::backend::Reference) {
m.reset(new RooMinimizer(*nllRef));
} else if (code == RooFitADBenchmarksUtils::backend::CodeSquashNumDiff) {
RooMinimizer::Config minimizerCfgNoAd;
minimizerCfgNoAd.useGradient = false;
m.reset(new RooMinimizer(*nllFunc, minimizerCfgNoAd));
m.reset(new RooMinimizer(*nllFuncNoGrad));
} else if (code == RooFitADBenchmarksUtils::backend::BatchMode) {
m.reset(new RooMinimizer(*nllRefBatch));
} else if (code == RooFitADBenchmarksUtils::backend::CodeSquashAD) {
Expand Down
6 changes: 3 additions & 3 deletions root/roofit/roofit/benchRooFitBackends.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ void runFitBenchmark(benchmark::State &state, RooAbsPdf &pdf, RooAbsData &data)
using namespace RooFit;
for (auto _ : state) {
if (runConfig == fitScalar) {
pdf.fitTo(data, BatchMode("off"), Minimizer(minimizerName), PrintLevel(printLevel - 1), PrintEvalErrors(-1));
pdf.fitTo(data, EvalBackend::Legacy(), Minimizer(minimizerName), PrintLevel(printLevel - 1), PrintEvalErrors(-1));
} else if (runConfig == fitCpu) {
pdf.fitTo(data, BatchMode("cpu"), Minimizer(minimizerName), PrintLevel(printLevel - 1), PrintEvalErrors(-1));
pdf.fitTo(data, EvalBackend::Cpu(), Minimizer(minimizerName), PrintLevel(printLevel - 1), PrintEvalErrors(-1));
} else if (runConfig == fitCuda) {
pdf.fitTo(data, BatchMode("cuda"), Minimizer(minimizerName), PrintLevel(printLevel - 1), PrintEvalErrors(-1));
pdf.fitTo(data, EvalBackend::Cuda(), Minimizer(minimizerName), PrintLevel(printLevel - 1), PrintEvalErrors(-1));
}
state.PauseTiming();
params.assign(paramsInitial);
Expand Down

0 comments on commit 09841fb

Please sign in to comment.