xref: /aosp_15_r20/external/executorch/devtools/bundled_program/bundled_program.h (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #pragma once
10 
11 #include <executorch/runtime/core/memory_allocator.h>
12 #include <executorch/runtime/executor/method.h>
13 
14 namespace executorch {
15 namespace bundled_program {
16 
17 /**
18  * An opaque pointer to a serialized bundled program.
19  */
20 using SerializedBundledProgram = const void;
21 
22 /**
23  * Load testset_idx-th bundled input of method_idx-th Method test in
24  * bundled_program_ptr to given Method.
25  *
26  * @param[in] method The Method to verify.
27  * @param[in] bundled_program_ptr The bundled program contains expected output.
28  * @param[in] testset_idx  The index of input needs to be set into given Method.
29  *
30  * @returns Return Error::Ok if load successfully, or the error happens during
31  * execution.
32  */
33 ET_NODISCARD ::executorch::runtime::Error load_bundled_input(
34     ::executorch::runtime::Method& method,
35     SerializedBundledProgram* bundled_program_ptr,
36     size_t testset_idx);
37 
38 /**
39  * Compare the Method's output with testset_idx-th bundled expected
40  * output in method_idx-th Method test.
41  *
42  * @param[in] method The Method to extract outputs from.
43  * @param[in] bundled_program_ptr The bundled program contains expected output.
44  * @param[in] testset_idx  The index of expected output needs to be compared.
45  * @param[in] rtol Relative tolerance used for data comparsion.
46  * @param[in] atol Absolute tolerance used for data comparsion.
47  *
48  * @returns Return Error::Ok if two outputs match, or the error happens during
49  * execution.
50  */
51 ET_NODISCARD ::executorch::runtime::Error verify_method_outputs(
52     ::executorch::runtime::Method& method,
53     SerializedBundledProgram* bundled_program_ptr,
54     size_t testset_idx,
55     double rtol = 1e-5,
56     double atol = 1e-8);
57 
58 /**
59  * Finds the serialized ExecuTorch program data in the provided bundled program
60  * file data.
61  *
62  * The returned buffer is appropriate for constructing a
63  * torch::executor::Program.
64  *
65  * @param[in] file_data The contents of an ExecuTorch program or bundled program
66  *                      file.
67  * @param[in] file_data_len The length of file_data, in bytes.
68  * @param[out] out_program_data The serialized Program data, if found.
69  * @param[out] out_program_data_len The length of out_program_data, in bytes.
70  *
71  * @returns Error::Ok if the given file is bundled program, a program was found
72  * in it, and out_program_data/out_program_data_len point to the data. Other
73  * values on failure.
74  */
75 ET_NODISCARD ::executorch::runtime::Error get_program_data(
76     void* file_data,
77     size_t file_data_len,
78     const void** out_program_data,
79     size_t* out_program_data_len);
80 
81 /**
82  * Checks whether the given file is a bundled program.
83  *
84  * @param[in] file_data The contents of the given file.
85  * @param[in] file_data_len The length of file_data, in bytes.
86  *
87  * @returns true if the given file is a bundled program, false otherwise
88  */
89 bool is_bundled_program(void* file_data, size_t file_data_len);
90 
91 /// DEPRECATED: Use the version with the file_data_len parameter.
is_bundled_program(void * file_data)92 ET_DEPRECATED inline bool is_bundled_program(void* file_data) {
93   // 128 is enough data to contain the identifier in the flatbuffer header.
94   return is_bundled_program(file_data, 128);
95 }
96 
97 } // namespace bundled_program
98 } // namespace executorch
99 
100 namespace torch {
101 namespace executor {
102 namespace bundled_program {
103 // TODO(T197294990): Remove these deprecated aliases once all users have moved
104 // to the new `::executorch` namespaces.
105 using serialized_bundled_program =
106     ::executorch::bundled_program::SerializedBundledProgram;
107 
LoadBundledInput(::executorch::runtime::Method & method,serialized_bundled_program * bundled_program_ptr,size_t testset_idx)108 ET_NODISCARD inline ::executorch::runtime::Error LoadBundledInput(
109     ::executorch::runtime::Method& method,
110     serialized_bundled_program* bundled_program_ptr,
111     size_t testset_idx) {
112   return ::executorch::bundled_program::load_bundled_input(
113       method, bundled_program_ptr, testset_idx);
114 }
115 
116 ET_NODISCARD inline ::executorch::runtime::Error
117 VerifyResultWithBundledExpectedOutput(
118     ::executorch::runtime::Method& method,
119     serialized_bundled_program* bundled_program_ptr,
120     size_t testset_idx,
121     double rtol = 1e-5,
122     double atol = 1e-8) {
123   return ::executorch::bundled_program::verify_method_outputs(
124       method, bundled_program_ptr, testset_idx, rtol, atol);
125 }
126 
GetProgramData(void * file_data,size_t file_data_len,const void ** out_program_data,size_t * out_program_data_len)127 ET_NODISCARD inline ::executorch::runtime::Error GetProgramData(
128     void* file_data,
129     size_t file_data_len,
130     const void** out_program_data,
131     size_t* out_program_data_len) {
132   return ::executorch::bundled_program::get_program_data(
133       file_data, file_data_len, out_program_data, out_program_data_len);
134 }
135 
IsBundledProgram(void * file_data)136 inline bool IsBundledProgram(void* file_data) {
137   // 128 is enough data to contain the identifier in the flatbuffer header.
138   return ::executorch::bundled_program::is_bundled_program(file_data, 128);
139 }
140 } // namespace bundled_program
141 } // namespace executor
142 } // namespace torch
143