27#ifndef TMVA_DNN_TENSORDATALOADER
28#define TMVA_DNN_TENSORDATALOADER
56template <
typename Architecture_t>
59 using Matrix_t =
typename Architecture_t::Matrix_t;
81template <
typename Data_t,
typename Architecture_t>
92template <
typename Data_t,
typename Architecture_t>
129template <
typename Data_t,
typename Architecture_t>
134 using Matrix_t =
typename Architecture_t::Matrix_t;
155 TTensorDataLoader(
const Data_t &
data,
size_t nSamples,
size_t batchSize,
size_t batchDepth,
size_t batchHeight,
156 size_t batchWidth,
size_t nOutputFeatures,
size_t nStreams = 1);
179 template<
typename RNG>
191template <
typename Architecture_t>
194 : fInputTensor(inputTensor), fOutputMatrix(outputMatrix), fWeightMatrix(weightMatrix)
202template <
typename Data_t,
typename Architecture_t>
204 size_t batchDepth,
size_t batchHeight,
size_t batchWidth,
205 size_t nOutputFeatures,
size_t nStreams)
206 : fData(
data), fNSamples(nSamples), fBatchSize(batchSize), fBatchDepth(batchDepth), fBatchHeight(batchHeight),
207 fBatchWidth(batchWidth), fNOutputFeatures(nOutputFeatures), fBatchIndex(0), fNStreams(nStreams), fDeviceBuffers(),
208 fHostBuffers(), fSampleIndices()
226template <
typename Data_t,
typename Architecture_t>
229 fBatchIndex %= (fNSamples / fBatchSize);
231 size_t inputTensorSize = fBatchDepth * fBatchHeight * fBatchWidth;
232 size_t outputMatrixSize = fBatchSize * fNOutputFeatures;
233 size_t weightMatrixSize = fBatchSize;
235 size_t streamIndex = fBatchIndex % fNStreams;
239 HostBuffer_t inputHostBuffer = hostBuffer.GetSubBuffer(0, inputTensorSize);
240 HostBuffer_t outputHostBuffer = hostBuffer.GetSubBuffer(inputTensorSize, outputMatrixSize);
241 HostBuffer_t weightHostBuffer = hostBuffer.GetSubBuffer(inputTensorSize + outputMatrixSize, weightMatrixSize);
243 DeviceBuffer_t inputDeviceBuffer = deviceBuffer.GetSubBuffer(0, inputTensorSize);
244 DeviceBuffer_t outputDeviceBuffer = deviceBuffer.GetSubBuffer(inputTensorSize, outputMatrixSize);
245 DeviceBuffer_t weightDeviceBuffer = deviceBuffer.GetSubBuffer(inputTensorSize + outputMatrixSize, weightMatrixSize);
251 size_t sampleIndex = fBatchIndex * fBatchSize;
252 IndexIterator_t sampleIndexIterator = fSampleIndices.begin() + sampleIndex;
254 CopyTensorInput(inputHostBuffer, sampleIndexIterator);
255 CopyTensorOutput(outputHostBuffer, sampleIndexIterator);
256 CopyTensorWeights(weightHostBuffer, sampleIndexIterator);
258 deviceBuffer.CopyFrom(hostBuffer);
260 std::vector<Matrix_t> inputTensor;
261 size_t jump = fBatchHeight * fBatchWidth;
262 for (
size_t i = 0; i < fBatchDepth; i++) {
263 DeviceBuffer_t subInputDeviceBuffer = inputDeviceBuffer.GetSubBuffer(i * jump, jump);
264 inputTensor.emplace_back(subInputDeviceBuffer, fBatchHeight, fBatchWidth);
266 Matrix_t outputMatrix(outputDeviceBuffer, fBatchSize, fNOutputFeatures);
267 Matrix_t weightMatrix(weightDeviceBuffer, fBatchSize, fNOutputFeatures);
274template <
typename Data_t,
typename Architecture_t>
275template <
typename RNG>
278 std::shuffle(fSampleIndices.begin(), fSampleIndices.end(), rng);
TTensorBatchIterator(TTensorDataLoader< Data_t, Architecture_t > &tensorDataLoader, size_t index=0)
TTensorBatch< Architecture_t > operator*()
bool operator!=(const TTensorBatchIterator &other)
TTensorDataLoader< Data_t, Architecture_t > & fTensorDataLoader
TTensorBatchIterator operator++()
std::vector< Matrix_t > & GetInput()
Return the tensor representing the input data.
TTensorBatch & operator=(const TTensorBatch &)=default
TTensorBatch(TTensorBatch &&)=default
TTensorBatch & operator=(TTensorBatch &&)=default
Matrix_t & GetWeights()
Return the matrix holding the event weights.
TTensorBatch(std::vector< Matrix_t > &, Matrix_t &, Matrix_t &)
Matrix_t & GetOutput()
Return the matrix representing the output data.
TTensorBatch(const TTensorBatch &)=default
std::vector< Matrix_t > fInputTensor
The input tensor batch, one matrix one input.
Matrix_t fOutputMatrix
The output matrix representing the ground truth.
typename Architecture_t::Matrix_t Matrix_t
TTensorDataLoader(const TTensorDataLoader &)=default
void Shuffle(RNG &rng)
Shuffle the order of the samples in the batch.
size_t fNOutputFeatures
The number of outputs from the classifier/regressor.
std::vector< size_t > fSampleIndices
Ordering of the samples in the epoch.
std::vector< DeviceBuffer_t > fDeviceBuffers
The device buffers used to keep the input, output and weight data.
TTensorBatch< Architecture_t > GetTensorBatch()
Return the next batch from the training set.
TTensorDataLoader & operator=(const TTensorDataLoader &)=default
typename Architecture_t::DeviceBuffer_t DeviceBuffer_t
size_t fBatchWidth
The number of columns in each matrix.
void CopyTensorOutput(HostBuffer_t &buffer, IndexIterator_t begin)
Copy output matrix into the given host buffer.
size_t fBatchIndex
The index of the batch when there are multiple batches in parallel.
size_t fBatchHeight
The number od rows in each matrix.
std::vector< HostBuffer_t > fHostBuffers
The host buffers used to load the input, output and weight data.
TTensorDataLoader(TTensorDataLoader &&)=default
size_t fBatchSize
The size of a batch.
void CopyTensorWeights(HostBuffer_t &buffer, IndexIterator_t begin)
Copy weight matrix into the given host buffer.
typename Architecture_t::Matrix_t Matrix_t
typename Architecture_t::HostBuffer_t HostBuffer_t
size_t fBatchDepth
The number of matrices in the tensor.
size_t fNStreams
Number of buffer pairs.
const Data_t & fData
The data that should be loaded in the batches.
TTensorDataLoader & operator=(TTensorDataLoader &&)=default
size_t fNSamples
The total number of samples in the dataset.
TTensorDataLoader(const Data_t &data, size_t nSamples, size_t batchSize, size_t batchDepth, size_t batchHeight, size_t batchWidth, size_t nOutputFeatures, size_t nStreams=1)
Constructor.
void CopyTensorInput(HostBuffer_t &buffer, IndexIterator_t begin)
Copy input tensor into the given host buffer.
Class that contains all the data information.
std::tuple< const std::vector< TMatrixT< Double_t > > &, const TMatrixT< Double_t > &, const TMatrixT< Double_t > & > TensorInput
typename std::vector< size_t >::iterator IndexIterator_t
std::tuple< const std::vector< Event * > &, const DataSetInfo & > TMVAInput_t
Abstract ClassifierFactory template that handles arbitrary types.