Skip to content

Commit

Permalink
Add test for quantized model
Browse files Browse the repository at this point in the history
  • Loading branch information
ciaranbor committed Jul 19, 2024
1 parent 98aa94f commit d2f53c9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
2 changes: 2 additions & 0 deletions source/qnn/model.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cstdint>
#include <filesystem>
#include <memory>
#include <vector>

#include "edgerunner/model.hpp"

Expand All @@ -16,6 +17,7 @@
#include "edgerunner/qnn/config.hpp"
#include "edgerunner/qnn/model.hpp"
#include "edgerunner/qnn/tensor.hpp"
#include "edgerunner/tensor.hpp"

namespace edge::qnn {

Expand Down
3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ endif()

set(TEST_SOURCES
source/tflite_test.cpp source/tflite_from_buffer_test.cpp
source/tflite_bad_model_test.cpp source/tflite_delegate_test.cpp
source/tflite_quantized_test.cpp source/tflite_bad_model_test.cpp
source/tflite_delegate_test.cpp
)

if(edgerunner_ENABLE_GPU)
Expand Down
68 changes: 68 additions & 0 deletions test/source/tflite_quantized_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <cstddef>
#include <cstdint>
#include <string>
#include <vector>

#include <catch2/catch_test_macros.hpp>

#include "edgerunner/edgerunner.hpp"
#include "edgerunner/model.hpp"
#include "edgerunner/tensor.hpp"
#include "utils.hpp"

TEST_CASE("Tflite default runtime quantized (CPU)",
"[tflite][cpu][quantized]") {
const std::string modelPath =
"models/tflite/mobilenet_v3_large_quantized.tflite";

auto model = edge::createModel(modelPath);
REQUIRE(model != nullptr);
REQUIRE(std::string {"mobilenet_v3_large_quantized"} == model->name());

model->applyDelegate(edge::DELEGATE::CPU);
REQUIRE(model->getDelegate() == edge::DELEGATE::CPU);

REQUIRE(model->getPrecision() == edge::TensorType::UINT8);

const auto inputs = model->getInputs();
const auto numInputs = model->getNumInputs();
REQUIRE(numInputs == 1);
REQUIRE(numInputs == inputs.size());

const auto outputs = model->getOutputs();
const auto numOutputs = model->getNumOutputs();
REQUIRE(numOutputs == 1);
REQUIRE(numOutputs == outputs.size());

auto input = model->getInput(0);
REQUIRE(input->getName() == "image_tensor");
REQUIRE(input->getDimensions() == std::vector<size_t> {1, 224, 224, 3});
REQUIRE(input->getType() == edge::TensorType::UINT8);
REQUIRE(input.get() == inputs[0].get());

auto inputData = input->getTensorAs<uint8_t>();
REQUIRE(inputData.size() == input->getSize());

auto badInput = model->getInput(1);
REQUIRE(badInput == nullptr);

auto output = model->getOutput(0);
REQUIRE(output->getName() == "class_logits");
REQUIRE(output->getDimensions() == std::vector<size_t> {1, 1000});
REQUIRE(output->getType() == edge::TensorType::UINT8);
REQUIRE(output.get() == outputs[0].get());

auto outputBuffer = output->getTensorAs<uint8_t>();
REQUIRE(outputBuffer.size() == output->getSize());

auto badOutput = model->getOutput(1);
REQUIRE(badOutput == nullptr);

const auto executionStatus = model->execute();
REQUIRE(executionStatus == edge::STATUS::SUCCESS);

/* verify output buffer is persistent across execution */
const auto newOutputBuffer = model->getOutput(0)->getTensorAs<uint8_t>();
REQUIRE(outputBuffer.data() == newOutputBuffer.data());
REQUIRE(outputBuffer.size() == newOutputBuffer.size());
}
2 changes: 2 additions & 0 deletions test/source/tflite_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ TEST_CASE("Tflite default runtime (CPU)", "[tflite][cpu]") {
model->applyDelegate(edge::DELEGATE::CPU);
REQUIRE(model->getDelegate() == edge::DELEGATE::CPU);

REQUIRE(model->getPrecision() == edge::TensorType::FLOAT16);

const auto inputs = model->getInputs();
const auto numInputs = model->getNumInputs();
REQUIRE(numInputs == 1);
Expand Down

0 comments on commit d2f53c9

Please sign in to comment.