Skip to content

Commit 02efdc0

Browse files
committed
Add QNN context binary test
1 parent 6c682f9 commit 02efdc0

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

models/qnn/mobilenet_v3_small.bin

5.98 MB
Binary file not shown.

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ endif()
4949
if(edgerunner_ENABLE_NPU)
5050
list(APPEND TEST_SOURCES source/tflite_npu_test.cpp
5151
source/qnn_shared_library_npu_test.cpp
52+
source/qnn_context_binary_npu_test.cpp
5253
)
5354
endif()
5455

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <algorithm>
2+
#include <cstddef>
3+
#include <iterator>
4+
#include <string>
5+
#include <vector>
6+
7+
#include <catch2/benchmark/catch_benchmark.hpp>
8+
#include <catch2/catch_message.hpp>
9+
#include <catch2/catch_test_macros.hpp>
10+
11+
#include "edgerunner/edgerunner.hpp"
12+
#include "edgerunner/model.hpp"
13+
#include "edgerunner/tensor.hpp"
14+
#include "utils.hpp"
15+
16+
TEST_CASE("QNN context binary NPU runtime", "[qnn][context][npu]") {
17+
const std::string modelPath = "models/qnn/mobilenet_v3_small.bin";
18+
19+
auto model = edge::createModel(modelPath);
20+
REQUIRE(model != nullptr);
21+
REQUIRE(std::string {"mobilenet_v3_small"} == model->name());
22+
REQUIRE(model->getDelegate() == edge::DELEGATE::CPU);
23+
24+
/* ensure CPU and NPU inference have the same inputs */
25+
auto cpuInputData = model->getInput(0)->getTensorAs<float>();
26+
std::fill(cpuInputData.begin(), cpuInputData.end(), 0);
27+
28+
auto executionStatus = model->execute();
29+
CHECK(executionStatus == edge::STATUS::SUCCESS);
30+
31+
const auto cpuOutput = model->getOutput(0)->getTensorAs<float>();
32+
33+
/* applying a new delegate releases memory, so need to copy CPU output to
34+
* compare later */
35+
std::vector<float> cpuResult;
36+
cpuResult.reserve(cpuOutput.size());
37+
std::copy(
38+
cpuOutput.cbegin(), cpuOutput.cend(), std::back_inserter(cpuResult));
39+
40+
const auto delegateStatus = model->applyDelegate(edge::DELEGATE::NPU);
41+
REQUIRE(delegateStatus == edge::STATUS::SUCCESS);
42+
REQUIRE(model->getDelegate() == edge::DELEGATE::NPU);
43+
44+
const auto numInputs = model->getNumInputs();
45+
REQUIRE(numInputs == 1);
46+
47+
const auto numOutputs = model->getNumOutputs();
48+
REQUIRE(numOutputs == 1);
49+
50+
auto input = model->getInput(0);
51+
REQUIRE(input->getName() == "image_tensor");
52+
REQUIRE(input->getDimensions() == std::vector<size_t> {1, 224, 224, 3});
53+
REQUIRE(input->getType() == edge::TensorType::FLOAT32);
54+
55+
auto inputData = input->getTensorAs<float>();
56+
REQUIRE(inputData.size() == input->getSize());
57+
58+
/* ensure CPU and NPU inference have the same inputs */
59+
std::fill(inputData.begin(), inputData.end(), 0);
60+
61+
executionStatus = model->execute();
62+
REQUIRE(executionStatus == edge::STATUS::SUCCESS);
63+
64+
BENCHMARK("execution") {
65+
return model->execute();
66+
};
67+
68+
auto output = model->getOutput(0);
69+
REQUIRE(output->getName() == "class_logits");
70+
REQUIRE(output->getDimensions() == std::vector<size_t> {1, 1000});
71+
REQUIRE(output->getType() == edge::TensorType::FLOAT32);
72+
73+
auto outputData = output->getTensorAs<float>();
74+
REQUIRE(outputData.size() == output->getSize());
75+
76+
const auto mse = meanSquaredError(cpuResult, outputData);
77+
CAPTURE(mse);
78+
REQUIRE(mse < MseThreshold);
79+
}

test/source/qnn_shared_library_npu_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "edgerunner/tensor.hpp"
1414
#include "utils.hpp"
1515

16-
TEST_CASE("QNN NPU runtime", "[qnn][npu]") {
16+
TEST_CASE("QNN shared library NPU runtime", "[qnn][shared][npu]") {
1717
const std::string modelPath = "models/qnn/mobilenet_v3_small.so";
1818

1919
auto model = edge::createModel(modelPath);
@@ -66,7 +66,7 @@ TEST_CASE("QNN NPU runtime", "[qnn][npu]") {
6666
};
6767

6868
auto output = model->getOutput(0);
69-
REQUIRE(output->getName() == "output_0");
69+
REQUIRE(output->getName() == "class_logits");
7070
REQUIRE(output->getDimensions() == std::vector<size_t> {1, 1000});
7171
REQUIRE(output->getType() == edge::TensorType::FLOAT32);
7272

0 commit comments

Comments
 (0)