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 | #include "general_utils.h" |
| 17 | #include "model_runner.h" |
| 18 | |
| 19 | int main() |
| 20 | { |
| 21 | using namespace TosaReference; |
| 22 | |
| 23 | std::string test_root(std::string(PROJECT_ROOT) + "../examples/test_add_1x4x4x4_f32/"); |
| 24 | std::string tosa_model_file(test_root + "flatbuffer-tflite/test_add_1x4x4x4_f32.tosa"); |
| 25 | std::string input0_file(test_root + "placeholder_0.npy"); |
| 26 | std::string input1_file(test_root + "placeholder_1.npy"); |
| 27 | std::string expected_output_file(test_root + "tflite_result.npy"); |
| 28 | |
| 29 | std::vector<std::string> input_names = { "TosaInput_0", "TosaInput_1" }; |
| 30 | std::string output_name = "TosaOutput_0"; |
| 31 | |
| 32 | std::vector<int32_t> input0_shape = { 1, 4, 4, 1 }; |
| 33 | std::vector<int32_t> input1_shape = { 1, 4, 4, 4 }; |
| 34 | std::vector<int32_t> output_shape = { 1, 4, 4, 4 }; |
| 35 | |
| 36 | std::vector<std::vector<float>> inputs(input_names.size()); |
| 37 | std::vector<float> expected_outputs = { }; |
| 38 | std::vector<float> actual_outputs = { }; |
| 39 | |
| 40 | // Read in inputs and expected outputs for sample purposes. |
| 41 | inputs[0] = readFromNpyFile<float>(input0_file.c_str(), input0_shape); |
| 42 | inputs[1] = readFromNpyFile<float>(input1_file.c_str(), input1_shape); |
| 43 | expected_outputs = readFromNpyFile<float>(expected_output_file.c_str(), output_shape); |
| 44 | |
| 45 | tosa::TosaSerializationHandler handler; |
| 46 | tosa::tosa_err_t error = handler.LoadFileTosaFlatbuffer(tosa_model_file.c_str()); |
| 47 | if(error != tosa::TOSA_OK) |
| 48 | { |
| 49 | WARNING("An error occurred while loading the model from file."); |
| 50 | return 1; |
| 51 | } |
| 52 | GraphStatus status; |
| 53 | |
| 54 | // Initialize the ModelRunner with configurations. |
| 55 | IModelRunner runner; |
| 56 | status = runner.initialize(handler); |
| 57 | if(status != GraphStatus::TOSA_VALID) |
| 58 | { |
| 59 | WARNING("An error occurred while initializing."); |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | // Set the model inputs using the input names and input data. |
| 64 | runner.setInput(input_names[0], inputs[0]); |
| 65 | runner.setInput(input_names[1], inputs[1]); |
| 66 | |
| 67 | // Run the ModelRunner using test inputs. |
| 68 | status = runner.run(); |
| 69 | if(status != GraphStatus::TOSA_VALID) |
| 70 | { |
| 71 | WARNING("An error occurred when running the model."); |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | // Get the outputs from the model. |
| 76 | actual_outputs = runner.getOutput<float>(output_name); |
| 77 | |
| 78 | // Compare the actual output to the expected output. |
| 79 | bool if_accurate = true; |
| 80 | for (size_t i = 0; i < expected_outputs.size(); ++i) |
| 81 | { |
| 82 | if(actual_outputs[i] != expected_outputs[i]) |
| 83 | { |
| 84 | WARNING("Actual output (%f) doesn't match expected output (%f)."); |
| 85 | if_accurate = false; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if(!if_accurate) |
| 90 | { |
| 91 | WARNING("There were mismatches in actual vs expected output, see above output for more details."); |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | printf("The model ran successfully without errors and matched the expected output.\n"); |
| 96 | return 0; |
| 97 | } |