19#include "output_transform.hxx"
33const int num_max_nodes = 100;
34const int num_max_edges = 300;
35const int NODE_FEATURE_SIZE = 4;
36const int EDGE_FEATURE_SIZE = 4;
37const int GLOBAL_FEATURE_SIZE = 1;
38const int LATENT_SIZE = 100;
40double check_mem(std::string s =
""){
42 printf(
"%s - ",s.c_str());
44 printf(
" Rmem = %8.3f MB, Vmem = %8.f3 MB \n",
45 p.fMemResident /1024.,
48 return p.fMemResident / 1024.;
55 std::vector<float> node_data;
56 std::vector<float> edge_data;
57 std::vector<float> global_data;
58 std::vector<int64_t> receivers;
59 std::vector<int64_t> senders;
63std::vector<float> ConcatenateFeatures(
const std::vector<float> &
a,
const std::vector<float> &
b,
size_t rows)
65 size_t fa =
a.size() / rows, fb =
b.size() / rows;
66 std::vector<float> out(rows * (fa + fb));
67 for (
size_t i = 0; i < rows; i++) {
68 std::copy(
a.begin() + i * fa,
a.begin() + (i + 1) * fa, out.begin() + i * (fa + fb));
69 std::copy(
b.begin() + i * fb,
b.begin() + (i + 1) * fb, out.begin() + i * (fa + fb) + fa);
76 TMVA_SOFIE_encoder::Session encoder{
"encoder.dat", num_max_edges, num_max_nodes};
77 TMVA_SOFIE_core::Session core{
"core.dat", num_max_edges, num_max_nodes};
78 TMVA_SOFIE_decoder::Session decoder{
"decoder.dat", num_max_edges, num_max_nodes};
79 TMVA_SOFIE_output_transform::Session output_transform{
"output_transform.dat", num_max_edges, num_max_nodes};
82 std::vector<std::vector<float>> Infer(
const GNN_Data &
d,
int nsteps) {
83 auto latent = encoder.infer(
d.num_nodes,
d.node_data.data(),
d.num_edges,
d.edge_data.data(),
84 d.global_data.data());
85 auto latent0 = latent;
86 std::vector<std::vector<float>> output;
87 for (
int i = 0; i < nsteps; i++) {
88 auto node_input = ConcatenateFeatures(latent0[0], latent[0],
d.num_nodes);
89 auto edge_input = ConcatenateFeatures(latent0[1], latent[1],
d.num_edges);
90 auto global_input = ConcatenateFeatures(latent0[2], latent[2], 1);
91 latent = core.infer(
d.num_nodes, node_input.data(),
d.num_edges, edge_input.data(),
92 global_input.data(),
d.receivers.data(),
d.senders.data());
93 auto decoded = decoder.infer(
d.num_nodes, latent[0].data(),
d.num_edges, latent[1].data(),
95 output = output_transform.infer(
d.num_nodes, decoded[0].data(),
d.num_edges, decoded[1].data(),
102std::vector<GNN_Data> ReadData(std::string treename, std::string
filename) {
109 int nevts = ndata.GetPtr()->
size();
110 std::vector<GNN_Data> dataSet;
111 dataSet.reserve(nevts);
112 for (
int i = 0; i < nevts; i++) {
114 auto &
n = (*(ndata.GetPtr()))[i];
115 auto &
e = (*(edata.GetPtr()))[i];
116 auto &
g = (*(gdata.GetPtr()))[i];
117 auto &
r = (*(rdata.GetPtr()))[i];
118 auto & s = (*(sdata.GetPtr()))[i];
119 gd.num_nodes =
n.size()/NODE_FEATURE_SIZE;
120 gd.num_edges =
e.size()/EDGE_FEATURE_SIZE;
121 gd.node_data.assign(
n.begin(),
n.end());
122 gd.edge_data.assign(
e.begin(),
e.end());
123 gd.global_data.assign(
g.begin(),
g.end());
124 gd.receivers.assign(
r.begin(),
r.end());
125 gd.senders.assign(s.begin(), s.end());
126 dataSet.emplace_back(std::move(gd));
132void TMVA_SOFIE_GNN_Application (
bool verbose =
false)
134 check_mem(
"Initial memory");
136 check_mem(
"After creating GNN");
138 const int nproc_steps = 5;
140 std::cout <<
"reading data\n";
141 auto inputData = ReadData(
"gdata",
"graph_data.root");
142 int nevts = inputData.size();
144 auto h1 =
new TH1D(
"h1_sofie",
"SOFIE Node data",40,1,0);
145 auto h2 =
new TH1D(
"h2_sofie",
"SOFIE Edge data",40,1,0);
146 auto h3 =
new TH1D(
"h3_sofie",
"SOFIE Global data",40,1,0);
147 std::cout <<
"doing inference...\n";
149 check_mem(
"Before evaluating");
151 for (
int i = 0; i < nevts; i++) {
152 auto result = gnn.Infer(inputData[i], nproc_steps);
160 check_mem(
"End evaluation");
161 auto c1 =
new TCanvas(
"c1",
"SOFIE Results");
164 c1->cd(2); h2->Draw();
165 c1->cd(3); h3->Draw();
168 auto c2 =
new TCanvas(
"c2",
"Reference Results");
170 auto o1 = file->Get<
TH1D>(
"h1");
171 auto o2 = file->Get<
TH1D>(
"h2");
172 auto o3 = file->Get<
TH1D>(
"h3");
174 c2->cd(1); o1->Draw();
180 std::cout <<
"SOFIE global mean " << h3->GetMean() <<
" reference " << o3->
GetMean() << std::endl;
181 if (std::abs(h3->GetMean() - o3->
GetMean()) > 5
e-4)
182 std::cerr <<
"Error in comparing SOFIE and reference results" << std::endl;
#define R__ADD_INCLUDE_PATH(PATH)
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
R__EXTERN TSystem * gSystem
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
A "std::vector"-like collection of values implementing handy operation to analyse them.
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
1-D histogram with a double per channel (see TH1 documentation)
virtual Double_t GetMean(Int_t axis=1) const
For axis = 1,2 or 3 returns the mean value of the histogram along X,Y or Z axis.
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
void Draw(Option_t *option="") override
Draw this histogram with options.
virtual Bool_t Divide(TF1 *f1, Double_t c1=1)
Performs the operation: this = this/(c1*f1) if errors are defined (see TH1::Sumw2),...
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
virtual int GetProcInfo(ProcInfo_t *info) const
Returns cpu and memory used by this process into the ProcInfo_t structure.
Double_t Mean(Long64_t n, const T *a, const Double_t *w=nullptr)
Returns the weighted mean of an array a with length n.