|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// This source code is licensed under the BSD-style license found in the |
| 5 | +// LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +#include <executorch/backends/native/examples/runner_utils/runner_utils.h> |
| 8 | + |
| 9 | +#include <algorithm> |
| 10 | +#include <cmath> |
| 11 | +#include <cstdio> |
| 12 | +#include <fstream> |
| 13 | +#include <functional> |
| 14 | +#include <limits> |
| 15 | +#include <numeric> |
| 16 | +#include <stdexcept> |
| 17 | + |
| 18 | +namespace ptn::runner_utils { |
| 19 | + |
| 20 | +std::vector<float> read_floats(const std::string& path) { |
| 21 | + std::ifstream file(path, std::ios::binary | std::ios::ate); |
| 22 | + if (!file) { |
| 23 | + throw std::runtime_error("cannot open " + path); |
| 24 | + } |
| 25 | + const std::streamsize size = file.tellg(); |
| 26 | + if (size < 0 || size % static_cast<std::streamsize>(sizeof(float)) != 0) { |
| 27 | + throw std::runtime_error("not a whole number of floats: " + path); |
| 28 | + } |
| 29 | + file.seekg(0, std::ios::beg); |
| 30 | + std::vector<float> out(static_cast<size_t>(size) / sizeof(float)); |
| 31 | + // cppcheck-suppress invalidPointerCast |
| 32 | + if (size > 0 && !file.read(reinterpret_cast<char*>(out.data()), size)) { |
| 33 | + throw std::runtime_error("cannot read " + path); |
| 34 | + } |
| 35 | + return out; |
| 36 | +} |
| 37 | + |
| 38 | +std::vector<std::string> read_lines(const std::string& path) { |
| 39 | + std::vector<std::string> lines; |
| 40 | + std::ifstream file(path); |
| 41 | + if (!file) { |
| 42 | + throw std::runtime_error("cannot open " + path); |
| 43 | + } |
| 44 | + std::string line; |
| 45 | + while (std::getline(file, line)) { |
| 46 | + lines.push_back(line); |
| 47 | + } |
| 48 | + return lines; |
| 49 | +} |
| 50 | + |
| 51 | +int64_t numel_of(const std::vector<int64_t>& sizes) { |
| 52 | + return std::accumulate( |
| 53 | + sizes.begin(), sizes.end(), int64_t{1}, std::multiplies<int64_t>()); |
| 54 | +} |
| 55 | + |
| 56 | +std::string shape_str(const std::vector<int64_t>& sizes) { |
| 57 | + std::string out = "["; |
| 58 | + for (size_t i = 0; i < sizes.size(); ++i) { |
| 59 | + out += (i == 0 ? "" : ", ") + std::to_string(sizes[i]); |
| 60 | + } |
| 61 | + return out + "]"; |
| 62 | +} |
| 63 | + |
| 64 | +std::vector<int> top_k(const std::vector<float>& values, int k) { |
| 65 | + std::vector<int> order(values.size()); |
| 66 | + std::iota(order.begin(), order.end(), 0); |
| 67 | + const int count = std::clamp<int>(k, 0, static_cast<int>(values.size())); |
| 68 | + std::partial_sort( |
| 69 | + order.begin(), order.begin() + count, order.end(), [&](int a, int b) { |
| 70 | + return values[a] > values[b]; |
| 71 | + }); |
| 72 | + order.resize(count); |
| 73 | + return order; |
| 74 | +} |
| 75 | + |
| 76 | +bool compare_floats( |
| 77 | + const std::vector<float>& actual, |
| 78 | + const std::vector<float>& expected, |
| 79 | + double rtol, |
| 80 | + double atol) { |
| 81 | + if (!std::isfinite(rtol) || !std::isfinite(atol) || rtol < 0 || atol < 0) { |
| 82 | + std::fprintf( |
| 83 | + stderr, |
| 84 | + "error: tolerances must be finite and non-negative (rtol=%g, " |
| 85 | + "atol=%g)\n", |
| 86 | + rtol, |
| 87 | + atol); |
| 88 | + return false; |
| 89 | + } |
| 90 | + if (actual.size() != expected.size()) { |
| 91 | + std::fprintf( |
| 92 | + stderr, |
| 93 | + "error: reference has %zu values, model produced %zu\n", |
| 94 | + expected.size(), |
| 95 | + actual.size()); |
| 96 | + return false; |
| 97 | + } |
| 98 | + size_t failures = 0; |
| 99 | + size_t worst_at = 0; |
| 100 | + double worst_excess = 0.0; |
| 101 | + double max_abs_err = 0.0; |
| 102 | + for (size_t i = 0; i < actual.size(); ++i) { |
| 103 | + if (!std::isfinite(actual[i]) || !std::isfinite(expected[i])) { |
| 104 | + ++failures; |
| 105 | + if (!std::isinf(worst_excess)) { |
| 106 | + worst_at = i; |
| 107 | + worst_excess = std::numeric_limits<double>::infinity(); |
| 108 | + } |
| 109 | + max_abs_err = std::numeric_limits<double>::infinity(); |
| 110 | + continue; |
| 111 | + } |
| 112 | + const double abs_err = std::fabs( |
| 113 | + static_cast<double>(actual[i]) - static_cast<double>(expected[i])); |
| 114 | + const double budget = |
| 115 | + atol + rtol * std::fabs(static_cast<double>(expected[i])); |
| 116 | + max_abs_err = std::max(max_abs_err, abs_err); |
| 117 | + if (abs_err > budget) { |
| 118 | + ++failures; |
| 119 | + if (abs_err - budget > worst_excess) { |
| 120 | + worst_excess = abs_err - budget; |
| 121 | + worst_at = i; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + std::printf( |
| 126 | + "compare: %zu values, max abs err %.6g, rtol %g atol %g\n", |
| 127 | + actual.size(), |
| 128 | + max_abs_err, |
| 129 | + rtol, |
| 130 | + atol); |
| 131 | + if (failures == 0) { |
| 132 | + return true; |
| 133 | + } |
| 134 | + std::printf( |
| 135 | + " %zu value(s) outside tolerance; worst at %zu: got %.6g, want %.6g\n", |
| 136 | + failures, |
| 137 | + worst_at, |
| 138 | + static_cast<double>(actual[worst_at]), |
| 139 | + static_cast<double>(expected[worst_at])); |
| 140 | + return false; |
| 141 | +} |
| 142 | + |
| 143 | +} // namespace ptn::runner_utils |
0 commit comments