blob: f1c9e96755b5de57498e2a7247825a3db186beef [file] [log] [blame]
Matthew Sloyanba5fad32022-09-26 13:31:43 +01001
Jerry Ge9c9c8da2023-07-19 23:08:16 +00002// Copyright (c) 2022-2023, ARM Limited.
Matthew Sloyanba5fad32022-09-26 13:31:43 +01003//
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
19int 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" };
Jerry Ge9c9c8da2023-07-19 23:08:16 +000030 std::string output_name = "TosaOutput_0";
Matthew Sloyanba5fad32022-09-26 13:31:43 +010031
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());
Jerry Ge9c9c8da2023-07-19 23:08:16 +000037 std::vector<float> expected_outputs = {};
38 std::vector<float> actual_outputs = {};
Matthew Sloyanba5fad32022-09-26 13:31:43 +010039
40 // Read in inputs and expected outputs for sample purposes.
Jerry Ge9c9c8da2023-07-19 23:08:16 +000041 inputs[0] = readFromNpyFile<float>(input0_file.c_str(), input0_shape);
42 inputs[1] = readFromNpyFile<float>(input1_file.c_str(), input1_shape);
Matthew Sloyanba5fad32022-09-26 13:31:43 +010043 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());
Jerry Ge9c9c8da2023-07-19 23:08:16 +000047 if (error != tosa::TOSA_OK)
Matthew Sloyanba5fad32022-09-26 13:31:43 +010048 {
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);
Jerry Ge9c9c8da2023-07-19 23:08:16 +000057 if (status != GraphStatus::TOSA_VALID)
Matthew Sloyanba5fad32022-09-26 13:31:43 +010058 {
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();
Jerry Ge9c9c8da2023-07-19 23:08:16 +000069 if (status != GraphStatus::TOSA_VALID)
Matthew Sloyanba5fad32022-09-26 13:31:43 +010070 {
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 {
Jerry Ge9c9c8da2023-07-19 23:08:16 +000082 if (actual_outputs[i] != expected_outputs[i])
Matthew Sloyanba5fad32022-09-26 13:31:43 +010083 {
84 WARNING("Actual output (%f) doesn't match expected output (%f).");
85 if_accurate = false;
86 }
87 }
88
Jerry Ge9c9c8da2023-07-19 23:08:16 +000089 if (!if_accurate)
Matthew Sloyanba5fad32022-09-26 13:31:43 +010090 {
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}