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)
112 std::vector<RFlat2DMatrix> fTrainingDatasets;
113 std::vector<RFlat2DMatrix> fValidationDatasets;
114
117
120
121 std::size_t fTrainingEpochCount{0};
122 std::size_t fValidationEpochCount{0};
123
124 std::vector<RColumnLayout> MakeColumnLayout();
125
126 /// \brief Opens a training or validation epoch and closes it again when done
127 struct REpochGuard {
130
131 REpochGuard(RDataLoaderEngine &engine, bool isTraining);
132
133 ~REpochGuard();
134 };
135
136public:
137 RDataLoaderEngine(const std::vector<ROOT::RDF::RNode> &rdfs, const std::size_t batchSize,
138 const std::size_t batchesInMemory, const std::vector<std::string> &cols,
139 const std::vector<std::size_t> &vecSizes = {}, const float vecPadding = 0.0,
140 const float testSize = 0.0, bool shuffle = true, bool dropRemainder = true,
141 const std::size_t setSeed = 0, bool loadEager = false, std::string sampleType = "",
142 float sampleRatio = 1.0, bool replacement = false);
143
145
146 void DeActivate();
147
148 void Activate();
149
150 void Save(std::string_view dataset_name, std::string_view filename, bool isTraining, std::string_view outputFormat);
151
153
155
157
159
160 void LoadData();
161
162 void CreateTrainBatches();
163
165
167
169
170 std::size_t NumberOfTrainingBatches();
171 std::size_t NumberOfValidationBatches();
172
173 std::size_t TrainRemainderRows();
174 std::size_t ValidationRemainderRows();
175
176 bool IsActive();
177
178 bool IsTrainingActive();
179
180 bool IsValidationActive();
181};
182
183} // namespace ROOT::Experimental::Internal::ML
184
185#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.
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.
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.
Opens a training or validation epoch and closes it again when done.
Wrapper around ROOT::RVec<float> representing a 2D matrix.