Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d6e46433 authored by Michael Butler's avatar Michael Butler Committed by Automerger Merge Worker
Browse files

Merge "Create conversions to/from NNAPI canonical types" am: f77f02bf am:...

Merge "Create conversions to/from NNAPI canonical types" am: f77f02bf am: b175b107 am: 367198e1

Original change: https://android-review.googlesource.com/c/platform/hardware/interfaces/+/1449138

Change-Id: I6c4d795c4d63593dba7061a979e0e441dbe52931
parents d312f177 367198e1
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
//
// Copyright (C) 2020 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

cc_library_static {
    name: "neuralnetworks_utils_hal_1_0",
    defaults: ["neuralnetworks_utils_defaults"],
    srcs: ["src/*"],
    local_include_dirs: ["include/nnapi/hal/1.0/"],
    export_include_dirs: ["include"],
    static_libs: [
        "neuralnetworks_types",
        "neuralnetworks_utils_hal_common",
    ],
    shared_libs: [
        "android.hardware.neuralnetworks@1.0",
    ],
    export_static_lib_headers: [
        "neuralnetworks_utils_hal_common",
    ],
}
+11 −0
Original line number Diff line number Diff line
# Neuralnetworks team
butlermichael@google.com
dgross@google.com
galarragas@google.com
jeanluc@google.com
levp@google.com
miaowang@google.com
pszczepaniak@google.com
slavash@google.com
vddang@google.com
xusongw@google.com
+66 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_CONVERSIONS_H
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_CONVERSIONS_H

#include <android/hardware/neuralnetworks/1.0/types.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>

namespace android::nn {

Result<OperandType> convert(const hal::V1_0::OperandType& operandType);
Result<OperationType> convert(const hal::V1_0::OperationType& operationType);
Result<Operand::LifeTime> convert(const hal::V1_0::OperandLifeTime& lifetime);
Result<DeviceStatus> convert(const hal::V1_0::DeviceStatus& deviceStatus);
Result<Capabilities::PerformanceInfo> convert(const hal::V1_0::PerformanceInfo& performanceInfo);
Result<Capabilities> convert(const hal::V1_0::Capabilities& capabilities);
Result<DataLocation> convert(const hal::V1_0::DataLocation& location);
Result<Operand> convert(const hal::V1_0::Operand& operand);
Result<Operation> convert(const hal::V1_0::Operation& operation);
Result<Model::OperandValues> convert(const hardware::hidl_vec<uint8_t>& operandValues);
Result<Memory> convert(const hardware::hidl_memory& memory);
Result<Model> convert(const hal::V1_0::Model& model);
Result<Request::Argument> convert(const hal::V1_0::RequestArgument& requestArgument);
Result<Request> convert(const hal::V1_0::Request& request);
Result<ErrorStatus> convert(const hal::V1_0::ErrorStatus& status);

}  // namespace android::nn

namespace android::hardware::neuralnetworks::V1_0::utils {

nn::Result<OperandType> convert(const nn::OperandType& operandType);
nn::Result<OperationType> convert(const nn::OperationType& operationType);
nn::Result<OperandLifeTime> convert(const nn::Operand::LifeTime& lifetime);
nn::Result<DeviceStatus> convert(const nn::DeviceStatus& deviceStatus);
nn::Result<PerformanceInfo> convert(const nn::Capabilities::PerformanceInfo& performanceInfo);
nn::Result<Capabilities> convert(const nn::Capabilities& capabilities);
nn::Result<DataLocation> convert(const nn::DataLocation& location);
nn::Result<Operand> convert(const nn::Operand& operand);
nn::Result<Operation> convert(const nn::Operation& operation);
nn::Result<hidl_vec<uint8_t>> convert(const nn::Model::OperandValues& operandValues);
nn::Result<hidl_memory> convert(const nn::Memory& memory);
nn::Result<Model> convert(const nn::Model& model);
nn::Result<RequestArgument> convert(const nn::Request::Argument& requestArgument);
nn::Result<hidl_memory> convert(const nn::Request::MemoryPool& memoryPool);
nn::Result<Request> convert(const nn::Request& request);
nn::Result<ErrorStatus> convert(const nn::ErrorStatus& status);

}  // namespace android::hardware::neuralnetworks::V1_0::utils

#endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_CONVERSIONS_H
+63 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_H
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_H

#include "nnapi/hal/1.0/Conversions.h"

#include <android-base/logging.h>
#include <android/hardware/neuralnetworks/1.0/types.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/Validation.h>

namespace android::hardware::neuralnetworks::V1_0::utils {

constexpr auto kVersion = nn::Version::ANDROID_OC_MR1;

template <typename Type>
nn::Result<void> validate(const Type& halObject) {
    const auto canonical = NN_TRY(nn::convert(halObject));
    const auto version = NN_TRY(nn::validate(canonical));
    if (version > utils::kVersion) {
        return NN_ERROR() << "";
    }
    return {};
}

template <typename Type>
bool valid(const Type& halObject) {
    const auto result = utils::validate(halObject);
    if (!result.has_value()) {
        LOG(ERROR) << result.error();
    }
    return result.has_value();
}

template <typename Type>
decltype(nn::convert(std::declval<Type>())) validatedConvertToCanonical(const Type& halObject) {
    auto canonical = NN_TRY(nn::convert(halObject));
    const auto version = NN_TRY(nn::validate(canonical));
    if (version > utils::kVersion) {
        return NN_ERROR() << "";
    }
    return canonical;
}

}  // namespace android::hardware::neuralnetworks::V1_0::utils

#endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_H
+122 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <android/hardware/neuralnetworks/1.0/types.h>
#include <nnapi/OperandTypes.h>
#include <nnapi/OperationTypes.h>
#include <nnapi/Types.h>
#include <type_traits>

namespace {

#define COMPARE_ENUMS_TYPES(lhsType, rhsType)                                                   \
    static_assert(                                                                              \
            std::is_same_v<                                                                     \
                    std::underlying_type_t<::android::hardware::neuralnetworks::V1_0::lhsType>, \
                    std::underlying_type_t<::android::nn::rhsType>>,                            \
            "::android::hardware::neuralnetworks::V1_0::" #lhsType                              \
            " does not have the same underlying type as ::android::nn::" #rhsType)

COMPARE_ENUMS_TYPES(OperandType, OperandType);
COMPARE_ENUMS_TYPES(OperationType, OperationType);
COMPARE_ENUMS_TYPES(ErrorStatus, ErrorStatus);
COMPARE_ENUMS_TYPES(OperandLifeTime, Operand::LifeTime);

#undef COMPARE_ENUMS_TYPES

#define COMPARE_ENUMS_FULL(lhsSymbol, rhsSymbol, lhsType, rhsType)                               \
    static_assert(                                                                               \
            static_cast<                                                                         \
                    std::underlying_type_t<::android::hardware::neuralnetworks::V1_0::lhsType>>( \
                    ::android::hardware::neuralnetworks::V1_0::lhsType::lhsSymbol) ==            \
                    static_cast<std::underlying_type_t<::android::nn::rhsType>>(                 \
                            ::android::nn::rhsType::rhsSymbol),                                  \
            "::android::hardware::neuralnetworks::V1_0::" #lhsType "::" #lhsSymbol               \
            " does not match ::android::nn::" #rhsType "::" #rhsSymbol)

#define COMPARE_ENUMS(symbol) COMPARE_ENUMS_FULL(symbol, symbol, OperandType, OperandType)

COMPARE_ENUMS(FLOAT32);
COMPARE_ENUMS(INT32);
COMPARE_ENUMS(UINT32);
COMPARE_ENUMS(TENSOR_FLOAT32);
COMPARE_ENUMS(TENSOR_INT32);
COMPARE_ENUMS(TENSOR_QUANT8_ASYMM);
COMPARE_ENUMS(OEM);
COMPARE_ENUMS(TENSOR_OEM_BYTE);

#undef COMPARE_ENUMS

#define COMPARE_ENUMS(symbol) COMPARE_ENUMS_FULL(symbol, symbol, OperationType, OperationType)

COMPARE_ENUMS(ADD);
COMPARE_ENUMS(AVERAGE_POOL_2D);
COMPARE_ENUMS(CONCATENATION);
COMPARE_ENUMS(CONV_2D);
COMPARE_ENUMS(DEPTHWISE_CONV_2D);
COMPARE_ENUMS(DEPTH_TO_SPACE);
COMPARE_ENUMS(DEQUANTIZE);
COMPARE_ENUMS(EMBEDDING_LOOKUP);
COMPARE_ENUMS(FLOOR);
COMPARE_ENUMS(FULLY_CONNECTED);
COMPARE_ENUMS(HASHTABLE_LOOKUP);
COMPARE_ENUMS(L2_NORMALIZATION);
COMPARE_ENUMS(L2_POOL_2D);
COMPARE_ENUMS(LOCAL_RESPONSE_NORMALIZATION);
COMPARE_ENUMS(LOGISTIC);
COMPARE_ENUMS(LSH_PROJECTION);
COMPARE_ENUMS(LSTM);
COMPARE_ENUMS(MAX_POOL_2D);
COMPARE_ENUMS(MUL);
COMPARE_ENUMS(RELU);
COMPARE_ENUMS(RELU1);
COMPARE_ENUMS(RELU6);
COMPARE_ENUMS(RESHAPE);
COMPARE_ENUMS(RESIZE_BILINEAR);
COMPARE_ENUMS(RNN);
COMPARE_ENUMS(SOFTMAX);
COMPARE_ENUMS(SPACE_TO_DEPTH);
COMPARE_ENUMS(SVDF);
COMPARE_ENUMS(TANH);
COMPARE_ENUMS(OEM_OPERATION);

#undef COMPARE_ENUMS

#define COMPARE_ENUMS(symbol) COMPARE_ENUMS_FULL(symbol, symbol, ErrorStatus, ErrorStatus)

COMPARE_ENUMS(NONE);
COMPARE_ENUMS(DEVICE_UNAVAILABLE);
COMPARE_ENUMS(GENERAL_FAILURE);
COMPARE_ENUMS(OUTPUT_INSUFFICIENT_SIZE);
COMPARE_ENUMS(INVALID_ARGUMENT);

#undef COMPARE_ENUMS

#define COMPARE_ENUMS(lhsSymbol, rhsSymbol) \
    COMPARE_ENUMS_FULL(lhsSymbol, rhsSymbol, OperandLifeTime, Operand::LifeTime)

COMPARE_ENUMS(TEMPORARY_VARIABLE, TEMPORARY_VARIABLE);
COMPARE_ENUMS(MODEL_INPUT, SUBGRAPH_INPUT);
COMPARE_ENUMS(MODEL_OUTPUT, SUBGRAPH_OUTPUT);
COMPARE_ENUMS(CONSTANT_COPY, CONSTANT_COPY);
COMPARE_ENUMS(CONSTANT_REFERENCE, CONSTANT_REFERENCE);
COMPARE_ENUMS(NO_VALUE, NO_VALUE);

#undef COMPARE_ENUMS

#undef COMPARE_ENUMS_FULL

}  // anonymous namespace
Loading