Skip to content

Commit 3ff625d

Browse files
committed
Add new version of SOFIEFunctor for RDF using variadic templates
1 parent 44ccf6d commit 3ff625d

File tree

1 file changed

+46
-22
lines changed

1 file changed

+46
-22
lines changed

root/tmva/sofie/RDF_SOFIE_Inference.cxx

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,46 @@
77

88
#include <benchmark/benchmark.h>
99

10-
// Functor to wrap SOFIE session to RDF functor signature
11-
template <typename Func>
12-
struct SofieFunctor {
13-
std::vector<float> input;
14-
std::vector<std::shared_ptr<Func>> sessions;
15-
SofieFunctor(unsigned nslots) :
16-
input(7*nslots,0) {
10+
// Functor to wrap SOFIE session to RDF functor signature
11+
12+
template <typename I, typename F, typename T>
13+
class SofieFunctorHelper;
14+
15+
template <std::size_t... N, typename S, typename T>
16+
class SofieFunctorHelper<std::index_sequence<N...>, S, T> {
17+
/// this is the magic to defined the operator () with N fixed parameter arguments
18+
template <std::size_t Idx>
19+
using AlwaysT = T;
20+
21+
std::vector<std::vector<T>> fInput;
22+
std::vector<std::shared_ptr<S>> fSessions;
23+
24+
public:
25+
26+
SofieFunctorHelper(int nslots) :
27+
fInput(nslots)
28+
{
1729
for (int i = 0; i < nslots; i++) {
18-
sessions.push_back(std::make_shared<Func>());
30+
fSessions.emplace_back(std::make_shared<S>());
1931
}
2032
}
2133

22-
double operator()(unsigned nslots, float x0, float x1, float x2, float x3, float x4, float x5, float x6) {
23-
int off = nslots*7;
24-
input[off] = x0;
25-
input[off+1] = x1;
26-
input[off+2] = x2;
27-
input[off+3] = x3;
28-
input[off+4] = x4;
29-
input[off+5] = x5;
30-
input[off+6] = x6;
31-
auto y = sessions[nslots]->infer(input.data()+nslots*7);
34+
double operator()(unsigned slot, AlwaysT<N>... args) {
35+
fInput[slot] = {args...};
36+
auto y = fSessions[slot]->infer(fInput[slot].data());
3237
return y[0];
3338
}
34-
39+
40+
3541
};
3642

43+
template <std::size_t N, typename F>
44+
auto SofieFunctor(int nslot) -> SofieFunctorHelper<std::make_index_sequence<N>, F, float>
45+
{
46+
return SofieFunctorHelper<std::make_index_sequence<N>, F, float>(nslot);
47+
}
48+
49+
3750
int NEVTS = -1;
3851
void BM_RDF_SOFIE_Inference(benchmark::State &state)
3952
{
@@ -53,23 +66,34 @@ void BM_RDF_SOFIE_Inference(benchmark::State &state)
5366
ROOT::RDataFrame df(treeName, fileName);
5467

5568

56-
SofieFunctor<TMVA_SOFIE_higgs_model_dense::Session> functor(nslot);
69+
//auto functor = SofieFunctor<TMVA_SOFIE_higgs_model_dense::Session,7>(nslot);
70+
// auto rdf_functor = [&](int slot, float x1, float x2, float x3, float x4, float x5, float x6, float x7){
71+
// return functor(slot, x1,x2,x3,x4,x5,x6,x7);
72+
// };
73+
SofieFunctorHelper<std::make_index_sequence<7>, TMVA_SOFIE_higgs_model_dense::Session, float> functor(nslot);
74+
75+
// test
76+
auto y = functor(0,1.,2.,3.,4.,5.,6.,7.);
77+
std::cout << y << std::endl;
5778

5879
std::vector<double> durations;
5980

6081
double ntot = 0;
6182

6283
for (auto _ : state) {
6384

64-
auto h1 = df.DefineSlot("DNN_Value", functor, {"m_jj", "m_jjj", "m_lv", "m_jlv", "m_bb", "m_wbb", "m_wwbb"})
85+
auto h1 = df.DefineSlot("DNN_Value", SofieFunctor<7,TMVA_SOFIE_higgs_model_dense::Session>(nslot), {"m_jj", "m_jjj", "m_lv", "m_jlv", "m_bb", "m_wbb", "m_wwbb"})
6586
.Histo1D("DNN_Value");
87+
// auto h1 = df.Define("DNN_Value", "functor(m_jj, m_jjj, m_lv,m_jlv, m_bb, m_wbb, m_wwbb)")
88+
// .Histo1D("DNN_Value");
6689

6790
auto t1 = std::chrono::high_resolution_clock::now();
6891

6992
auto n = h1->GetEntries();
93+
//int n = 100;
7094
auto t2 = std::chrono::high_resolution_clock::now();
7195
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
72-
96+
7397
durations.push_back(duration / 1.E6);
7498
NEVTS = n;
7599
ntot += n;

0 commit comments

Comments
 (0)