summaryrefslogtreecommitdiff
path: root/nn/common/include/nnapi/TypeUtils.h
diff options
context:
space:
mode:
authorMichael Butler <butlermichael@google.com>2020-10-27 21:06:37 -0700
committerMichael Butler <butlermichael@google.com>2020-11-04 04:11:33 +0000
commit2bd2cae2be76017277a7aae6b5fe4bc7fc1ff67c (patch)
tree7453b5e0852551022c3ba140674e60661facb938 /nn/common/include/nnapi/TypeUtils.h
parent9d8007c5342dcdd324af06af8ca18f6ea2974af1 (diff)
downloadml-2bd2cae2be76017277a7aae6b5fe4bc7fc1ff67c.tar.gz
Update NNAPI canonical validation
Previously, there was a gap in our validation code when it came to validating operations because it relied on code that was not compatible with the canonical types at the time. Now that the code is compatible, this CL closes the validation gap. To facilitate this, this CL also relocates some utility code (HalVersion, NN_RET_CHECK_*) from Utils.h to TypeUtils.h. Bug: 160668435 Test: mma Change-Id: I4b9ed9ad14f79ad84860809f72c0ccbd6399e6f0 Merged-In: I4b9ed9ad14f79ad84860809f72c0ccbd6399e6f0 (cherry picked from commit 5f02de4004735a216bf1726af22f3fe4ebb16c01)
Diffstat (limited to 'nn/common/include/nnapi/TypeUtils.h')
-rw-r--r--nn/common/include/nnapi/TypeUtils.h92
1 files changed, 92 insertions, 0 deletions
diff --git a/nn/common/include/nnapi/TypeUtils.h b/nn/common/include/nnapi/TypeUtils.h
index 77761c40b..9dc67cf46 100644
--- a/nn/common/include/nnapi/TypeUtils.h
+++ b/nn/common/include/nnapi/TypeUtils.h
@@ -17,6 +17,9 @@
#ifndef ANDROID_FRAMEWORKS_ML_NN_COMMON_NNAPI_TYPE_UTILS_H
#define ANDROID_FRAMEWORKS_ML_NN_COMMON_NNAPI_TYPE_UTILS_H
+#include <android-base/logging.h>
+#include <android-base/macros.h>
+
#include <ostream>
#include <utility>
#include <vector>
@@ -28,6 +31,15 @@
namespace android::nn {
+enum class HalVersion : int32_t {
+ UNKNOWN,
+ V1_0,
+ V1_1,
+ V1_2,
+ V1_3,
+ LATEST = V1_3,
+};
+
bool isExtension(OperandType type);
bool isExtension(OperationType type);
@@ -93,6 +105,7 @@ std::ostream& operator<<(std::ostream& os, const OptionalTimePoint& optionalTime
std::ostream& operator<<(std::ostream& os, const TimeoutDuration& timeoutDuration);
std::ostream& operator<<(std::ostream& os, const OptionalTimeoutDuration& optionalTimeoutDuration);
std::ostream& operator<<(std::ostream& os, const Version& version);
+std::ostream& operator<<(std::ostream& os, const HalVersion& halVersion);
bool operator==(const Timing& a, const Timing& b);
bool operator!=(const Timing& a, const Timing& b);
@@ -112,6 +125,85 @@ bool operator!=(const Operand& a, const Operand& b);
bool operator==(const Operation& a, const Operation& b);
bool operator!=(const Operation& a, const Operation& b);
+// The NN_RET_CHECK family of macros defined below is similar to the CHECK family defined in
+// system/libbase/include/android-base/logging.h
+//
+// The difference is that NN_RET_CHECK macros use LOG(ERROR) instead of LOG(FATAL)
+// and return false instead of aborting.
+
+// Logs an error and returns false. Append context using << after. For example:
+//
+// NN_RET_CHECK_FAIL() << "Something went wrong";
+//
+// The containing function must return a bool.
+#define NN_RET_CHECK_FAIL() \
+ return ::android::nn::FalseyErrorStream() \
+ << "NN_RET_CHECK failed (" << __FILE__ << ":" << __LINE__ << "): "
+
+// Logs an error and returns false if condition is false. Extra logging can be appended using <<
+// after. For example:
+//
+// NN_RET_CHECK(false) << "Something went wrong";
+//
+// The containing function must return a bool.
+#define NN_RET_CHECK(condition) \
+ while (UNLIKELY(!(condition))) NN_RET_CHECK_FAIL() << #condition << " "
+
+// Helper for NN_CHECK_xx(x, y) macros.
+#define NN_RET_CHECK_OP(LHS, RHS, OP) \
+ for (auto _values = ::android::base::MakeEagerEvaluator(LHS, RHS); \
+ UNLIKELY(!(_values.lhs.v OP _values.rhs.v)); \
+ /* empty */) \
+ NN_RET_CHECK_FAIL() \
+ << #LHS << " " << #OP << " " << #RHS << " (" << #LHS << " = " \
+ << ::android::base::LogNullGuard<decltype(_values.lhs.v)>::Guard(_values.lhs.v) \
+ << ", " << #RHS << " = " \
+ << ::android::base::LogNullGuard<decltype(_values.rhs.v)>::Guard(_values.rhs.v) \
+ << ") "
+
+// Logs an error and returns false if a condition between x and y does not hold. Extra logging can
+// be appended using << after. For example:
+//
+// NN_RET_CHECK_EQ(a, b) << "Something went wrong";
+//
+// The values must implement the appropriate comparison operator as well as
+// `operator<<(std::ostream&, ...)`.
+// The containing function must return a bool.
+#define NN_RET_CHECK_EQ(x, y) NN_RET_CHECK_OP(x, y, ==)
+#define NN_RET_CHECK_NE(x, y) NN_RET_CHECK_OP(x, y, !=)
+#define NN_RET_CHECK_LE(x, y) NN_RET_CHECK_OP(x, y, <=)
+#define NN_RET_CHECK_LT(x, y) NN_RET_CHECK_OP(x, y, <)
+#define NN_RET_CHECK_GE(x, y) NN_RET_CHECK_OP(x, y, >=)
+#define NN_RET_CHECK_GT(x, y) NN_RET_CHECK_OP(x, y, >)
+
+// Ensure that every user of FalseyErrorStream is linked to the
+// correct instance, using the correct LOG_TAG
+namespace {
+
+// A wrapper around LOG(ERROR) that can be implicitly converted to bool (always evaluates to false).
+// Used to implement stream logging in NN_RET_CHECK.
+class FalseyErrorStream {
+ DISALLOW_COPY_AND_ASSIGN(FalseyErrorStream);
+
+ public:
+ FalseyErrorStream() {}
+
+ template <typename T>
+ FalseyErrorStream& operator<<(const T& value) {
+ mBuffer << value;
+ return *this;
+ }
+
+ ~FalseyErrorStream() { LOG(ERROR) << mBuffer.str(); }
+
+ operator bool() const { return false; }
+
+ private:
+ std::ostringstream mBuffer;
+};
+
+} // namespace
+
} // namespace android::nn
#endif // ANDROID_FRAMEWORKS_ML_NN_COMMON_NNAPI_TYPE_UTILS_H