SampleDriverQuant.cpp revision ef22aa5727b96e9a0863ef71cfbe3dbdac339408
1/*
2 * Copyright (C) 2017 The Android Open Source Project
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
17#define LOG_TAG "SampleDriverQuant"
18
19#include "SampleDriver.h"
20
21#include "HalInterfaces.h"
22#include "Utils.h"
23
24#include <android-base/logging.h>
25#include <hidl/LegacySupport.h>
26#include <thread>
27
28namespace android {
29namespace nn {
30namespace sample_driver {
31
32class SampleDriverQuant : public SampleDriver {
33public:
34    SampleDriverQuant() : SampleDriver("sample-quant") {}
35    Return<void> getCapabilities(getCapabilities_cb _hidl_cb) override;
36    Return<void> getSupportedOperations(const Model& model, getSupportedOperations_cb cb) override;
37};
38
39Return<void> SampleDriverQuant::getCapabilities(getCapabilities_cb cb) {
40    LOG(DEBUG) << "getCapabilities()";
41    Capabilities capabilities = {.float32Performance = {.execTime = 50.0f, .powerUsage = 1.0f},
42                                 .quantized8Performance = {.execTime = 50.0f, .powerUsage = 1.0f}};
43    cb(ErrorStatus::NONE, capabilities);
44    return Void();
45}
46
47Return<void> SampleDriverQuant::getSupportedOperations(const Model& model,
48                                                       getSupportedOperations_cb cb) {
49    LOG(DEBUG) << "getSupportedOperations()";
50    if (validateModel(model)) {
51        const size_t count = model.operations.size();
52        std::vector<bool> supported(count);
53        for (size_t i = 0; i < count; i++) {
54            const Operation& operation = model.operations[i];
55            if (operation.inputs.size() > 0) {
56                const Operand& firstOperand = model.operands[operation.inputs[0]];
57                supported[i] = firstOperand.type == OperandType::TENSOR_QUANT8_ASYMM;
58            }
59        }
60        cb(ErrorStatus::NONE, supported);
61    } else {
62        std::vector<bool> supported;
63        cb(ErrorStatus::INVALID_ARGUMENT, supported);
64    }
65    return Void();
66}
67
68} // namespace sample_driver
69} // namespace nn
70} // namespace android
71
72using android::nn::sample_driver::SampleDriverQuant;
73using android::sp;
74
75int main() {
76    SetMinimumLogSeverity(android::base::VERBOSE);
77    sp<SampleDriverQuant> driver(new SampleDriverQuant());
78    return driver->run();
79}
80