Skip to content

Commit 180c176

Browse files
committed
Updating tutorial task
1 parent 08826b2 commit 180c176

1 file changed

Lines changed: 89 additions & 3 deletions

File tree

‎Tutorials/ML/benchmarkOnnxModel.cxx‎

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222

2323
#include <algorithm>
2424
#include <chrono>
25+
#include <cmath>
2526
#include <cstddef>
2627
#include <cstdint>
2728
#include <fstream>
2829
#include <iomanip>
2930
#include <ios>
3031
#include <limits>
3132
#include <memory>
33+
#include <sstream>
3234
#include <stdexcept>
3335
#include <string>
3436
#include <vector>
@@ -116,6 +118,8 @@ struct BenchmarkOnnxModel {
116118
Configurable<int> iterations{"iterations", 100, "Timed inference calls"};
117119
Configurable<int> cpuThreads{"cpu-threads", 1, "ORT intra-op CPU threads"};
118120
Configurable<std::string> outputCsv{"output-csv", "onnx-benchmark.csv", "CSV output (overwritten)"};
121+
Configurable<std::string> inputFile{"input-file", "", "Optional whitespace-separated feature matrix [batch-size, features]; empty uses synthetic inputs"};
122+
Configurable<std::string> predictionsFile{"predictions-file", "", "Optional first-output matrix as text (overwritten); extra inference outside timing"};
119123

120124
void run(ProcessingContext& pc)
121125
{
@@ -189,17 +193,55 @@ struct BenchmarkOnnxModel {
189193
if (count > std::numeric_limits<size_t>::max() / sizeof(float)) {
190194
throw std::runtime_error("Input size overflow");
191195
}
192-
// Replace this feature filling with AO2D extraction and training preprocessing later.
196+
// File loading and FP16 conversion are outside the timing loop. Without a
197+
// file, retain deterministic synthetic inputs for performance-only runs.
193198
std::vector<float> floats(fp16.value ? 0 : count);
194199
std::vector<o2::OrtDataType::Float16_t> halves(fp16.value ? count : 0);
195200
static_assert(sizeof(o2::OrtDataType::Float16_t) == 2);
196-
for (size_t i = 0; i < count; ++i) {
197-
float value = static_cast<float>(i % 101) / 100.f - 0.5f;
201+
auto store = [&](size_t i, float value) {
202+
if (!std::isfinite(value)) {
203+
throw std::runtime_error("Input features must be finite");
204+
}
198205
if (fp16.value) {
199206
halves[i] = o2::OrtDataType::Float16_t(value);
207+
if (!std::isfinite(halves[i].ToFloat())) {
208+
throw std::runtime_error("Input feature overflows FP16");
209+
}
200210
} else {
201211
floats[i] = value;
202212
}
213+
};
214+
if (inputFile.value.empty()) {
215+
for (size_t i = 0; i < count; ++i) {
216+
store(i, static_cast<float>(i % 101) / 100.f - 0.5f);
217+
}
218+
} else {
219+
std::ifstream file(inputFile.value);
220+
if (!file) {
221+
throw std::runtime_error("Cannot open input-file: " + inputFile.value);
222+
}
223+
std::string line;
224+
for (int row = 0; row < batchSize.value; ++row) {
225+
if (!std::getline(file, line)) {
226+
throw std::runtime_error("input-file has fewer rows than batch-size");
227+
}
228+
std::istringstream values(line);
229+
for (int column = 0; column < features.value; ++column) {
230+
float value = 0;
231+
if (!(values >> value)) {
232+
throw std::runtime_error("input-file has a missing or invalid feature at row " + std::to_string(row + 1));
233+
}
234+
store(static_cast<size_t>(row) * features.value + column, value);
235+
}
236+
values >> std::ws;
237+
if (!values.eof()) {
238+
throw std::runtime_error("input-file has extra columns at row " + std::to_string(row + 1));
239+
}
240+
}
241+
file >> std::ws;
242+
if (!file.eof() || file.bad()) {
243+
throw std::runtime_error("input-file has extra rows or a read error");
244+
}
203245
}
204246
auto host = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
205247
void* data = fp16.value ? static_cast<void*>(halves.data()) : static_cast<void*>(floats.data());
@@ -238,6 +280,50 @@ struct BenchmarkOnnxModel {
238280
if (memory) {
239281
afterRun = memory->used();
240282
}
283+
if (!predictionsFile.value.empty()) {
284+
// A separate, untimed run leaves benchmark allocation/destruction intact.
285+
auto outputs = session.Run(runOptions, inputNames, &input, 1, outputNames.data(), outputNames.size());
286+
auto const& output = outputs.front();
287+
if (!output.IsTensor()) {
288+
throw std::runtime_error("Prediction export requires a dense tensor as the first output");
289+
}
290+
auto info = output.GetTensorTypeAndShapeInfo();
291+
auto outputShape = info.GetShape();
292+
auto outputType = info.GetElementType();
293+
if (outputShape.size() != 2 || outputShape[0] != batchSize.value || outputShape[1] <= 0 ||
294+
(outputType != ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT && outputType != ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16)) {
295+
throw std::runtime_error("Prediction export requires an FP32 or FP16 first output [batch-size, classes]");
296+
}
297+
const float* fp32 = outputType == ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT ? output.GetTensorData<float>() : nullptr;
298+
const auto* fp16Data = outputType == ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16 ? output.GetTensorData<o2::OrtDataType::Float16_t>() : nullptr;
299+
const size_t columns = static_cast<size_t>(outputShape[1]);
300+
// Validate before opening the destination, so invalid predictions do not
301+
// replace an existing file with a partial matrix.
302+
auto valueAt = [&](size_t i) { return fp32 ? fp32[i] : fp16Data[i].ToFloat(); };
303+
for (size_t i = 0; i < info.GetElementCount(); ++i) {
304+
if (!std::isfinite(valueAt(i))) {
305+
throw std::runtime_error("Prediction export found a nonfinite value");
306+
}
307+
}
308+
std::ofstream predictions(predictionsFile.value, std::ios::trunc);
309+
if (!predictions) {
310+
throw std::runtime_error("Cannot open predictions-file: " + predictionsFile.value);
311+
}
312+
predictions << std::setprecision(std::numeric_limits<float>::max_digits10);
313+
for (int row = 0; row < batchSize.value; ++row) {
314+
for (size_t column = 0; column < columns; ++column) {
315+
if (column) {
316+
predictions << ' ';
317+
}
318+
predictions << valueAt(static_cast<size_t>(row) * columns + column);
319+
}
320+
predictions << '\n';
321+
}
322+
predictions.close();
323+
if (!predictions) {
324+
throw std::runtime_error("Failed writing predictions-file");
325+
}
326+
}
241327
std::ofstream csv(outputCsv.value, std::ios::trunc);
242328
if (!csv) {
243329
throw std::runtime_error("Cannot open output-csv");

0 commit comments

Comments
 (0)