Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RDataLoaderEngine.hxx
Go to the documentation of this file.
1// Author: Dante Niewenhuis, VU Amsterdam 07/2023
2// Author: Kristupas Pranckietis, Vilnius University 05/2024
3// Author: Nopphakorn Subsa-Ard, King Mongkut's University of Technology Thonburi (KMUTT) (TH) 08/2024
4// Author: Vincenzo Eduardo Padulano, CERN 10/2024
5// Author: Martin Føll, University of Oslo (UiO) & CERN 01/2026
6// Author: Silia Taider, CERN 02/2026
7
8/*************************************************************************
9 * Copyright (C) 1995-2026, Rene Brun and Fons Rademakers. *
10 * All rights reserved. *
11 * *
12 * For the licensing terms see $ROOTSYS/LICENSE. *
13 * For the list of contributors see $ROOTSYS/README/CREDITS. *
14 *************************************************************************/
15
16#ifndef ROOT_INTERNAL_ML_RDATALOADERENGINE
17#define ROOT_INTERNAL_ML_RDATALOADERENGINE
18
19#include <condition_variable>
20#include <memory>
21#include <mutex>
22#include <string>
23#include <string_view>
24#include <thread>
25#include <vector>
26
28
29namespace ROOT {
30namespace Detail {
31namespace RDF {
32class RNodeBase;
33}
34} // namespace Detail
35namespace RDF {
36template <typename Proxied>
37class RInterface;
38using RNode = RInterface<::ROOT::Detail::RDF::RNodeBase>;
39} // namespace RDF
40} // namespace ROOT
41
42// Empty namespace to create a hook for the Pythonization
44}
45
47
48class RBatchLoader;
49class RClusterLoader;
50class RDatasetLoader;
52class RSampler;
53struct RColumnLayout;
54
55/**
56 \class ROOT::Experimental::Internal::ML::RDataLoaderEngine
57\brief
58
59In this class, the processes of loading clusters (see RClusterLoader) and creating batches from those clusters (see
60RBatchLoader) are combined, allowing batches from the training and validation sets to be loaded directly from a dataset
61in an RDataFrame.
62*/
63
65private:
66 std::vector<std::string> fCols;
67 std::vector<std::size_t> fVecSizes;
68 std::vector<RColumnLayout> fColumnLayout;
69 std::size_t fBatchSize;
70 std::size_t fSetSeed;
71
72 // buffer quantities
73 std::size_t fBufferCapacity;
74 std::size_t fLowWatermark;
75
76 std::size_t fTrainingClusterIdx{0};
77 std::size_t fValidationClusterIdx{0};
78
79 float fTestSize;
80
81 std::unique_ptr<RDatasetLoader> fDatasetLoader;
82 std::unique_ptr<RClusterLoader> fClusterLoader;
83 std::unique_ptr<RBatchLoader> fTrainingBatchLoader;
84 std::unique_ptr<RBatchLoader> fValidationBatchLoader;
85 std::unique_ptr<RSampler> fTrainingSampler;
86 std::unique_ptr<RSampler> fValidationSampler;
87
88 std::unique_ptr<RFlat2DMatrixOperators> fTensorOperators;
89
90 std::vector<ROOT::RDF::RNode> fRdfs;
91
92 std::unique_ptr<std::thread> fLoadingThread;
93 std::condition_variable fLoadingCondition;
94 std::mutex fLoadingMutex;
95
99 std::string fSampleType;
102
103 bool fIsActive{false}; // Whether the loading thread is active
104
107
110
111 // flattened buffers for chunks and temporary tensors (rows * cols)
113
114 // rows of fDataset in the training and validation sets, one set per dataframe when sampling
115 std::vector<std::vector<std::size_t>> fTrainingRows;
116 std::vector<std::vector<std::size_t>> fValidationRows;
117
120
121 std::size_t fTrainingEpochCount{0};
122 std::size_t fValidationEpochCount{0};
123
124 /// \brief Rows of an eager epoch, turned into batches as they are requested
125 struct REagerEpoch {
126 // non-owning pointer: the data stays in the engine's own members (fDataset for the normal path,
127 // fSampledTraining/ValidationDataset for the sampler path)
128 const RFlat2DMatrix *fSource = nullptr;
129 std::vector<std::size_t> fPermutation;
130 std::size_t fNextRow = 0;
131 std::size_t fEndRow = 0;
132 };
133
136
137 REagerEpoch MakeEagerEpoch(const std::vector<std::size_t> &rows);
138
139 void CreateNextEagerBatch(RBatchLoader &batchLoader, REagerEpoch &epoch);
140
141 std::vector<RColumnLayout> MakeColumnLayout();
142
143 /// \brief Opens a training or validation epoch and closes it again when done
144 struct REpochGuard {
147
148 REpochGuard(RDataLoaderEngine &engine, bool isTraining);
149
150 ~REpochGuard();
151 };
152
153public:
154 RDataLoaderEngine(const std::vector<ROOT::RDF::RNode> &rdfs, const std::size_t batchSize,
155 const std::size_t batchesInMemory, const std::vector<std::string> &cols,
156 const std::vector<std::size_t> &vecSizes = {}, const float vecPadding = 0.0,
157 const float testSize = 0.0, bool shuffle = true, bool dropRemainder = true,
158 const std::size_t setSeed = 0, bool loadEager = false, std::string sampleType = "",
159 float sampleRatio = 1.0, bool replacement = false);
160
162
163 void DeActivate();
164
165 void Activate();
166
167 void Save(std::string_view dataset_name, std::string_view filename, bool isTraining, std::string_view outputFormat);
168
170
172
174
176
177 void LoadData();
178
179 void CreateTrainBatches();
180
182
184
186
187 std::size_t NumberOfTrainingBatches();
188 std::size_t NumberOfValidationBatches();
189
190 std::size_t TrainRemainderRows();
191 std::size_t ValidationRemainderRows();
192
193 bool IsActive();
194
195 bool IsTrainingActive();
196
197 bool IsValidationActive();
198};
199
200} // namespace ROOT::Experimental::Internal::ML
201
202#endif // ROOT_INTERNAL_ML_RDATALOADERENGINE
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
Building and loading the batches from loaded chunks in RChunkLoader.
Loads TTree/RNTuple clusters from one or more RDataFrames into RFlat2DMatrix buffers for ML training ...
In this class, the processes of loading clusters (see RClusterLoader) and creating batches from those...
std::unique_ptr< RFlat2DMatrixOperators > fTensorOperators
RFlat2DMatrix GetValidationBatch()
Loads a validation batch from the queue.
void Activate()
Activate the loading process by spawning the loading thread.
void Save(std::string_view dataset_name, std::string_view filename, bool isTraining, std::string_view outputFormat)
Materialize one train/test split to disk by draining a full epoch through the normal batch pipeline a...
std::vector< RColumnLayout > MakeColumnLayout()
Describe how the loader's columns map onto a batch-tensor row.
std::vector< std::vector< std::size_t > > fValidationRows
void LoadData()
Main loop for loading clusters and creating batches.
void ActivateTrainingEpoch()
Activate the training epoch by starting the batchloader.
void CreateValidationBatches()
Creates validation batches by first loading a chunk (see RClusterLoader), and then split it into batc...
RDataLoaderEngine(const std::vector< ROOT::RDF::RNode > &rdfs, const std::size_t batchSize, const std::size_t batchesInMemory, const std::vector< std::string > &cols, const std::vector< std::size_t > &vecSizes={}, const float vecPadding=0.0, const float testSize=0.0, bool shuffle=true, bool dropRemainder=true, const std::size_t setSeed=0, bool loadEager=false, std::string sampleType="", float sampleRatio=1.0, bool replacement=false)
RFlat2DMatrix GetTrainBatch()
Loads a training batch from the queue.
std::vector< std::vector< std::size_t > > fTrainingRows
REagerEpoch MakeEagerEpoch(const std::vector< std::size_t > &rows)
The eager epoch reading the given rows of the dataset, in shuffled order when shuffling.
void CreateNextEagerBatch(RBatchLoader &batchLoader, REagerEpoch &epoch)
Create the next batch of an eager epoch, unless one is already queued or the epoch is done.
void CreateTrainBatches()
Create training batches by first loading a chunk (see RClusterLoader) and split it into batches (see ...
Load the whole dataset into memory.
Collection of operations applied to one or multiple flat 2D matrices.
Implementation of different sampling strategies.
Definition RSampler.hxx:32
RInterface<::ROOT::Detail::RDF::RNodeBase > RNode
Where one of the loader's columns lives inside a batch-tensor row and how it's shaped.
Rows of an eager epoch, turned into batches as they are requested.
Opens a training or validation epoch and closes it again when done.
Wrapper around ROOT::RVec<float> representing a 2D matrix.