Matthew Sloyan | ba5fad3 | 2022-09-26 13:31:43 +0100 | [diff] [blame] | 1 | |
| 2 | // Copyright (c) 2022, ARM Limited. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | #ifndef MODEL_RUNNER_H_ |
| 17 | #define MODEL_RUNNER_H_ |
| 18 | |
| 19 | #include "model_common.h" |
| 20 | #include "graph_status.h" |
| 21 | |
| 22 | #include "tosa_serialization_handler.h" |
| 23 | |
| 24 | namespace TosaReference |
| 25 | { |
| 26 | |
| 27 | class ModelRunnerImpl; |
| 28 | |
| 29 | /* |
| 30 | * This interface allows a user to initialize, run and get the output from a model. |
| 31 | * See model_runner_simple_sample.cpp for example on how this interface can be used. |
| 32 | */ |
| 33 | class IModelRunner |
| 34 | { |
| 35 | public: |
| 36 | IModelRunner(); |
| 37 | IModelRunner(const func_config_t& func_config, const func_debug_t& func_debug); |
| 38 | |
| 39 | ~IModelRunner(); |
| 40 | |
| 41 | /* |
| 42 | * Functional and debug configurations can also be set. |
| 43 | * See func_config.h and func_debug.h for possible options. |
| 44 | */ |
| 45 | void setFuncConfig(func_config_t& func_config); |
| 46 | void setFuncDebug(func_debug_t& func_debug); |
| 47 | |
| 48 | /* |
| 49 | * Initialize the model. |
| 50 | * The TosaSerializationHandler is validated and then converted to a SubgraphTraverser internally. |
| 51 | * This SubgraphTraverser is initialized, allocated and validated. |
| 52 | * The status of the graph will be returned upon completion. |
| 53 | */ |
| 54 | GraphStatus initialize(tosa::TosaSerializationHandler& serialization_handler); |
| 55 | |
| 56 | /* |
| 57 | * Run the model using the internal SubgraphTraverser created during initialization. |
| 58 | * If validate_only is specified run() will simply return the graph status. |
| 59 | * Otherwise, the graph will be run and the output tensors will be generated if the graph is valid. |
| 60 | * The output tensors can then be retrieved with getOutput(). |
| 61 | * NOTE: initialize() must be called before run(). Also, setInput() must be called for all inputs in the model. |
| 62 | */ |
| 63 | GraphStatus run(); |
| 64 | |
| 65 | /* |
| 66 | * Set the input tensors for the model. |
| 67 | * The input_name much match the input tensor name in the model. |
| 68 | * NOTE: setInput() must be called for each input tensor before run() is called. |
| 69 | */ |
| 70 | template <typename T> |
Matthew Sloyan | 2e4d889 | 2022-10-18 18:02:48 +0100 | [diff] [blame] | 71 | int setInput(std::string input_name, std::vector<T>& vals); |
Matthew Sloyan | ba5fad3 | 2022-09-26 13:31:43 +0100 | [diff] [blame] | 72 | |
| 73 | /* |
Grant Watson | 64285a1 | 2022-11-16 15:32:39 +0000 | [diff] [blame] | 74 | * Set the input tensors for the model through a raw byte buffer. |
| 75 | * The input_name much match the input tensor name in the model. |
| 76 | * NOTE: setInput() must be called for each input tensor before run() is called. |
| 77 | */ |
| 78 | int setInput(std::string input_name, uint8_t* raw_ptr, size_t size); |
| 79 | |
| 80 | /* |
Matthew Sloyan | ba5fad3 | 2022-09-26 13:31:43 +0100 | [diff] [blame] | 81 | * Retrieve the output tensors from the graph after running. |
| 82 | * The output_name much match the output tensor name in the model. |
| 83 | * NOTE: run() must be called before outputs are retrieved. |
| 84 | */ |
| 85 | template <typename T> |
| 86 | std::vector<T> getOutput(std::string output_name); |
| 87 | |
Grant Watson | 64285a1 | 2022-11-16 15:32:39 +0000 | [diff] [blame] | 88 | /* |
| 89 | * Retrieve the output tensors from the graph after running in a raw byte buffer. |
| 90 | * The output_name much match the output tensor name in the model. |
| 91 | * NOTE: run() must be called before outputs are retrieved. |
| 92 | */ |
| 93 | int getOutput(std::string output_name, uint8_t* raw_ptr, size_t size); |
| 94 | |
Matthew Sloyan | ba5fad3 | 2022-09-26 13:31:43 +0100 | [diff] [blame] | 95 | private: |
| 96 | std::unique_ptr<ModelRunnerImpl> model_runner_impl; |
| 97 | }; |
| 98 | |
| 99 | }; // namespace TosaReference |
| 100 | |
| 101 | #endif |