Home | History | Annotate | Download | only in sample
      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 "SampleDriverFloatFast"
     18 
     19 #include "SampleDriver.h"
     20 
     21 #include "HalInterfaces.h"
     22 #include "Utils.h"
     23 #include "ValidateHal.h"
     24 
     25 #include <android-base/logging.h>
     26 #include <hidl/LegacySupport.h>
     27 #include <thread>
     28 
     29 namespace android {
     30 namespace nn {
     31 namespace sample_driver {
     32 
     33 class SampleDriverFloatFast : public SampleDriver {
     34 public:
     35     SampleDriverFloatFast() : SampleDriver("sample-float-fast") {}
     36     Return<void> getCapabilities_1_1(getCapabilities_1_1_cb cb) override;
     37     Return<void> getSupportedOperations_1_1(const V1_1::Model& model,
     38                                             getSupportedOperations_1_1_cb cb) override;
     39 };
     40 
     41 Return<void> SampleDriverFloatFast::getCapabilities_1_1(getCapabilities_1_1_cb cb) {
     42     android::nn::initVLogMask();
     43     VLOG(DRIVER) << "getCapabilities()";
     44     Capabilities capabilities = {.float32Performance = {.execTime = 0.8f, .powerUsage = 1.2f},
     45                                  .quantized8Performance = {.execTime = 1.0f, .powerUsage = 1.0f},
     46                                  .relaxedFloat32toFloat16Performance =
     47                                      {.execTime = 0.7f, .powerUsage = 1.1f}};
     48     cb(ErrorStatus::NONE, capabilities);
     49     return Void();
     50 }
     51 
     52 Return<void> SampleDriverFloatFast::getSupportedOperations_1_1(const V1_1::Model& model,
     53                                                                getSupportedOperations_1_1_cb cb) {
     54     VLOG(DRIVER) << "getSupportedOperations()";
     55     if (validateModel(model)) {
     56         const size_t count = model.operations.size();
     57         std::vector<bool> supported(count);
     58         for (size_t i = 0; i < count; i++) {
     59             const Operation& operation = model.operations[i];
     60             if (operation.inputs.size() > 0) {
     61                 const Operand& firstOperand = model.operands[operation.inputs[0]];
     62                 supported[i] = firstOperand.type == OperandType::TENSOR_FLOAT32;
     63             }
     64         }
     65         cb(ErrorStatus::NONE, supported);
     66     } else {
     67         std::vector<bool> supported;
     68         cb(ErrorStatus::INVALID_ARGUMENT, supported);
     69     }
     70     return Void();
     71 }
     72 
     73 } // namespace sample_driver
     74 } // namespace nn
     75 } // namespace android
     76 
     77 using android::nn::sample_driver::SampleDriverFloatFast;
     78 using android::sp;
     79 
     80 int main() {
     81     sp<SampleDriverFloatFast> driver(new SampleDriverFloatFast());
     82     return driver->run();
     83 }
     84