|
| 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/vision/ClassificationRunner.h> |
| 8 | + |
| 9 | +#include <cstdint> |
| 10 | +#include <cstdio> |
| 11 | +#include <exception> |
| 12 | +#include <memory> |
| 13 | +#include <string> |
| 14 | +#include <vector> |
| 15 | + |
| 16 | +#include <executorch/backends/native/examples/runner_utils/runner_utils.h> |
| 17 | +#include <executorch/backends/native/runtime/Program.h> |
| 18 | +#include <executorch/backends/native/runtime/deserialize/Package.h> |
| 19 | +#include <executorch/backends/native/runtime/engine/Engine.h> |
| 20 | +#include <executorch/backends/native/runtime/graph/ScalarType.h> |
| 21 | +#include <gflags/gflags.h> |
| 22 | + |
| 23 | +DEFINE_string(package, "", "Path to the *.ptn to run. Required."); |
| 24 | +DEFINE_string(input, "", "Path to the input tensor, raw float32. Required."); |
| 25 | +DEFINE_string( |
| 26 | + expected, |
| 27 | + "", |
| 28 | + "Reference logits to compare against, raw float32."); |
| 29 | +DEFINE_string(categories, "", "Newline-separated class labels, for display."); |
| 30 | +DEFINE_string(method, "", "Method to run. Defaults to the first one."); |
| 31 | +DEFINE_double(rtol, 1e-3, "Relative tolerance for the logits comparison."); |
| 32 | +DEFINE_double(atol, 1e-3, "Absolute tolerance for the logits comparison."); |
| 33 | +namespace { |
| 34 | + |
| 35 | +std::string label_of(const std::vector<std::string>& labels, int index) { |
| 36 | + if (index >= 0 && index < static_cast<int>(labels.size())) { |
| 37 | + return labels[index]; |
| 38 | + } |
| 39 | + return "class_" + std::to_string(index); |
| 40 | +} |
| 41 | + |
| 42 | +void print_top5( |
| 43 | + const std::vector<float>& logits, |
| 44 | + const std::vector<std::string>& labels) { |
| 45 | + const std::vector<int> order = ptn::runner_utils::top_k(logits, 5); |
| 46 | + std::printf("top-5 (class : logit):\n"); |
| 47 | + for (const int index : order) { |
| 48 | + std::printf( |
| 49 | + " %4d %-28s %10.4f\n", |
| 50 | + index, |
| 51 | + label_of(labels, index).c_str(), |
| 52 | + logits[index]); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +} // namespace |
| 57 | + |
| 58 | +namespace ptn { |
| 59 | + |
| 60 | +int run_classification( |
| 61 | + const std::function<std::unique_ptr<EngineHost>()>& make_host, |
| 62 | + const std::function<ConstantBytes(const EngineContext&)>& constant_bytes) { |
| 63 | + if (FLAGS_package.empty() || FLAGS_input.empty()) { |
| 64 | + std::fprintf(stderr, "error: --package and --input are required\n"); |
| 65 | + return 1; |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + const auto package = |
| 70 | + std::make_shared<const ptn::Package>(ptn::Package::load(FLAGS_package)); |
| 71 | + const ptn::ByteSpan program_bytes = package->program_bytes(); |
| 72 | + const auto program = std::make_shared<const ptn::Program>( |
| 73 | + ptn::Program::load(program_bytes.data(), program_bytes.size())); |
| 74 | + |
| 75 | + const std::vector<std::string> names = program->method_names(); |
| 76 | + if (names.empty()) { |
| 77 | + std::fprintf(stderr, "error: package holds no methods\n"); |
| 78 | + return 2; |
| 79 | + } |
| 80 | + const std::string method_name = |
| 81 | + FLAGS_method.empty() ? names[0] : FLAGS_method; |
| 82 | + std::printf( |
| 83 | + "package: %s (program %zu bytes, %zu constants / %zu bytes)\n", |
| 84 | + FLAGS_package.c_str(), |
| 85 | + program_bytes.size(), |
| 86 | + package->keys().size(), |
| 87 | + package->constant_bytes()); |
| 88 | + |
| 89 | + const auto host = make_host(); |
| 90 | + const auto context = host->create_context(program, package); |
| 91 | + std::printf( |
| 92 | + "engine: %s on %s\n", |
| 93 | + host->name().c_str(), |
| 94 | + host->device_name().c_str()); |
| 95 | + |
| 96 | + const std::unique_ptr<ptn::EngineExecutable> executable = |
| 97 | + context->compile(method_name); |
| 98 | + std::printf( |
| 99 | + "method: %s (%zu inputs, %zu outputs)\n", |
| 100 | + method_name.c_str(), |
| 101 | + executable->num_inputs(), |
| 102 | + executable->num_outputs()); |
| 103 | + const ConstantBytes constants = constant_bytes(*context); |
| 104 | + std::printf( |
| 105 | + "constants: %zu unique source bytes, %zu materialized\n", |
| 106 | + constants.unique, |
| 107 | + constants.materialized); |
| 108 | + |
| 109 | + if (executable->num_inputs() != 1 || executable->num_outputs() != 1) { |
| 110 | + std::fprintf( |
| 111 | + stderr, |
| 112 | + "error: expected 1 input and 1 output, got %zu and %zu\n", |
| 113 | + executable->num_inputs(), |
| 114 | + executable->num_outputs()); |
| 115 | + return 2; |
| 116 | + } |
| 117 | + if (executable->input_dtype(0) != ptn::kFloat || |
| 118 | + executable->output_dtype(0) != ptn::kFloat) { |
| 119 | + std::fprintf(stderr, "error: this runner handles fp32 models only\n"); |
| 120 | + return 2; |
| 121 | + } |
| 122 | + |
| 123 | + const std::vector<int64_t> in_sizes = executable->input_sizes(0); |
| 124 | + const std::vector<int64_t> out_sizes = executable->output_sizes(0); |
| 125 | + std::printf( |
| 126 | + " input: %s\n output: %s\n", |
| 127 | + ptn::runner_utils::shape_str(in_sizes).c_str(), |
| 128 | + ptn::runner_utils::shape_str(out_sizes).c_str()); |
| 129 | + |
| 130 | + const std::vector<float> input = |
| 131 | + ptn::runner_utils::read_floats(FLAGS_input); |
| 132 | + if (static_cast<int64_t>(input.size()) != |
| 133 | + ptn::runner_utils::numel_of(in_sizes)) { |
| 134 | + std::fprintf( |
| 135 | + stderr, |
| 136 | + "error: input has %zu floats, the model wants %lld\n", |
| 137 | + input.size(), |
| 138 | + static_cast<long long>(ptn::runner_utils::numel_of(in_sizes))); |
| 139 | + return 2; |
| 140 | + } |
| 141 | + |
| 142 | + executable->set_input(0, input.data(), input.size(), ptn::kFloat); |
| 143 | + executable->execute(); |
| 144 | + |
| 145 | + std::vector<float> logits( |
| 146 | + static_cast<size_t>(ptn::runner_utils::numel_of(out_sizes))); |
| 147 | + executable->get_output(0, logits.data(), logits.size(), ptn::kFloat); |
| 148 | + if (logits.empty()) { |
| 149 | + std::fprintf(stderr, "error: model produced no logits\n"); |
| 150 | + return 2; |
| 151 | + } |
| 152 | + |
| 153 | + std::vector<std::string> labels; |
| 154 | + if (!FLAGS_categories.empty()) { |
| 155 | + labels = ptn::runner_utils::read_lines(FLAGS_categories); |
| 156 | + } |
| 157 | + print_top5(logits, labels); |
| 158 | + const int argmax = ptn::runner_utils::top_k(logits, 1).front(); |
| 159 | + std::printf( |
| 160 | + "argmax: %d (%s)\n", argmax, label_of(labels, argmax).c_str()); |
| 161 | + |
| 162 | + if (FLAGS_expected.empty()) { |
| 163 | + std::printf("result: ran (no reference given)\n"); |
| 164 | + return 0; |
| 165 | + } |
| 166 | + const std::vector<float> reference = |
| 167 | + ptn::runner_utils::read_floats(FLAGS_expected); |
| 168 | + const bool pass = ptn::runner_utils::compare_floats( |
| 169 | + logits, reference, FLAGS_rtol, FLAGS_atol); |
| 170 | + std::printf("result: %s\n", pass ? "PASS" : "FAIL"); |
| 171 | + return pass ? 0 : 3; |
| 172 | + } catch (const std::exception& e) { |
| 173 | + std::fprintf(stderr, "error: %s\n", e.what()); |
| 174 | + return 2; |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +} // namespace ptn |
0 commit comments