|
22 | 22 |
|
23 | 23 | #include <algorithm> |
24 | 24 | #include <chrono> |
| 25 | +#include <cmath> |
25 | 26 | #include <cstddef> |
26 | 27 | #include <cstdint> |
27 | 28 | #include <fstream> |
28 | 29 | #include <iomanip> |
29 | 30 | #include <ios> |
30 | 31 | #include <limits> |
31 | 32 | #include <memory> |
| 33 | +#include <sstream> |
32 | 34 | #include <stdexcept> |
33 | 35 | #include <string> |
34 | 36 | #include <vector> |
@@ -116,6 +118,8 @@ struct BenchmarkOnnxModel { |
116 | 118 | Configurable<int> iterations{"iterations", 100, "Timed inference calls"}; |
117 | 119 | Configurable<int> cpuThreads{"cpu-threads", 1, "ORT intra-op CPU threads"}; |
118 | 120 | 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"}; |
119 | 123 |
|
120 | 124 | void run(ProcessingContext& pc) |
121 | 125 | { |
@@ -189,17 +193,55 @@ struct BenchmarkOnnxModel { |
189 | 193 | if (count > std::numeric_limits<size_t>::max() / sizeof(float)) { |
190 | 194 | throw std::runtime_error("Input size overflow"); |
191 | 195 | } |
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. |
193 | 198 | std::vector<float> floats(fp16.value ? 0 : count); |
194 | 199 | std::vector<o2::OrtDataType::Float16_t> halves(fp16.value ? count : 0); |
195 | 200 | 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 | + } |
198 | 205 | if (fp16.value) { |
199 | 206 | halves[i] = o2::OrtDataType::Float16_t(value); |
| 207 | + if (!std::isfinite(halves[i].ToFloat())) { |
| 208 | + throw std::runtime_error("Input feature overflows FP16"); |
| 209 | + } |
200 | 210 | } else { |
201 | 211 | floats[i] = value; |
202 | 212 | } |
| 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 | + } |
203 | 245 | } |
204 | 246 | auto host = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault); |
205 | 247 | void* data = fp16.value ? static_cast<void*>(halves.data()) : static_cast<void*>(floats.data()); |
@@ -238,6 +280,50 @@ struct BenchmarkOnnxModel { |
238 | 280 | if (memory) { |
239 | 281 | afterRun = memory->used(); |
240 | 282 | } |
| 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 | + } |
241 | 327 | std::ofstream csv(outputCsv.value, std::ios::trunc); |
242 | 328 | if (!csv) { |
243 | 329 | throw std::runtime_error("Cannot open output-csv"); |
|
0 commit comments