|
1 | | -// LiteRT native C API smoke: link libLiteRt and exercise the C API entry point — |
2 | | -// create + destroy a LiteRtEnvironment (no model needed). The link proves the packaged |
3 | | -// lib is symbol-complete (LiteRt* symbols resolve); the run proves it loads + initializes. |
4 | | -// (A full forward pass — LiteRtCreateCompiledModel + LiteRtRunCompiledModel on a .tflite — |
5 | | -// comes once the per-platform Bazel build is green.) |
| 1 | +// LiteRT native C API smoke: link libLiteRt and exercise the real consumption path — |
| 2 | +// create an environment, LOAD a .tflite model (both from file and from a memory buffer), |
| 3 | +// and COMPILE it for the CPU accelerator. The link proves the packaged lib is symbol-complete; |
| 4 | +// the run proves the packaged headers match the binary's ABI and that model loading works. |
6 | 5 | // |
7 | | -// Usage: smoke (exit 0 = pass) |
| 6 | +// This guards against a header/binary version skew: LiteRtCreateModelFrom{File,Buffer} gained a |
| 7 | +// leading LiteRtEnvironment parameter upstream, so headers from a different commit than the lib |
| 8 | +// silently mis-shift the arguments — model load then opens '' (status 500) or segfaults. Env |
| 9 | +// create/destroy alone does NOT catch that; loading + compiling a real model does. |
| 10 | +// |
| 11 | +// (Numerical correctness of the forward pass is validated end-to-end in anira's test suite.) |
| 12 | +// |
| 13 | +// Usage: smoke <path/to/model.tflite> (exit 0 = pass) |
8 | 14 | #include <cstdio> |
| 15 | +#include <fstream> |
| 16 | +#include <vector> |
| 17 | + |
9 | 18 | #include "litert/c/litert_common.h" |
10 | 19 | #include "litert/c/litert_environment.h" |
| 20 | +#include "litert/c/litert_model.h" |
| 21 | +#include "litert/c/litert_options.h" |
| 22 | +#include "litert/c/litert_compiled_model.h" |
| 23 | + |
| 24 | +static int fail(const char* what, LiteRtStatus s) { |
| 25 | + std::printf("FAIL: %s (status=%d)\n", what, static_cast<int>(s)); |
| 26 | + return 1; |
| 27 | +} |
| 28 | + |
| 29 | +int main(int argc, char** argv) { |
| 30 | + if (argc < 2) { std::printf("usage: smoke <model.tflite>\n"); return 1; } |
| 31 | + const char* path = argv[1]; |
11 | 32 |
|
12 | | -int main() { |
13 | 33 | LiteRtEnvironment env = nullptr; |
14 | 34 | LiteRtStatus s = LiteRtCreateEnvironment(/*num_options=*/0, /*options=*/nullptr, &env); |
15 | | - if (s != kLiteRtStatusOk || env == nullptr) { |
16 | | - std::printf("FAIL: LiteRtCreateEnvironment status=%d\n", static_cast<int>(s)); |
17 | | - return 1; |
18 | | - } |
| 35 | + if (s != kLiteRtStatusOk || env == nullptr) return fail("LiteRtCreateEnvironment", s); |
| 36 | + |
| 37 | + // 1) Load from file. |
| 38 | + LiteRtModel model_file = nullptr; |
| 39 | + s = LiteRtCreateModelFromFile(env, path, &model_file); |
| 40 | + if (s != kLiteRtStatusOk || model_file == nullptr) return fail("LiteRtCreateModelFromFile", s); |
| 41 | + |
| 42 | + // 2) Load the same bytes from a memory buffer (must outlive the model). |
| 43 | + std::ifstream f(path, std::ios::binary | std::ios::ate); |
| 44 | + if (!f) { std::printf("FAIL: cannot read %s\n", path); return 1; } |
| 45 | + const std::streamsize n = f.tellg(); |
| 46 | + f.seekg(0); |
| 47 | + std::vector<char> buf(static_cast<size_t>(n)); |
| 48 | + f.read(buf.data(), n); |
| 49 | + LiteRtModel model_buf = nullptr; |
| 50 | + s = LiteRtCreateModelFromBuffer(env, buf.data(), static_cast<size_t>(n), &model_buf); |
| 51 | + if (s != kLiteRtStatusOk || model_buf == nullptr) return fail("LiteRtCreateModelFromBuffer", s); |
| 52 | + |
| 53 | + // 3) Compile (CPU) — exercises the rest of the model-consumption ABI. |
| 54 | + LiteRtOptions opts = nullptr; |
| 55 | + s = LiteRtCreateOptions(&opts); |
| 56 | + if (s != kLiteRtStatusOk) return fail("LiteRtCreateOptions", s); |
| 57 | + s = LiteRtSetOptionsHardwareAccelerators(opts, kLiteRtHwAcceleratorCpu); |
| 58 | + if (s != kLiteRtStatusOk) return fail("LiteRtSetOptionsHardwareAccelerators", s); |
| 59 | + LiteRtCompiledModel compiled = nullptr; |
| 60 | + s = LiteRtCreateCompiledModel(env, model_file, opts, &compiled); |
| 61 | + if (s != kLiteRtStatusOk || compiled == nullptr) return fail("LiteRtCreateCompiledModel", s); |
| 62 | + |
| 63 | + LiteRtDestroyCompiledModel(compiled); |
| 64 | + LiteRtDestroyOptions(opts); |
| 65 | + LiteRtDestroyModel(model_buf); |
| 66 | + LiteRtDestroyModel(model_file); |
19 | 67 | LiteRtDestroyEnvironment(env); |
20 | | - std::printf("PASS: LiteRt environment create/destroy OK\n"); |
| 68 | + |
| 69 | + std::printf("PASS: LiteRt env + model load (file+buffer) + CPU compile OK\n"); |
21 | 70 | return 0; |
22 | 71 | } |
0 commit comments