Add NNT_static internal tests for device memory allocation.
These tests use a customized IDevice to test the device memory
allocation and fallback logic.
This CL also allows the runtime to dispatch device memory allocation
with dynamic shape to drivers.
Additionally, this CL fixes a bug that a failed device memory allocation
will return BAD_DATA -- it should return OP_FAILED instead.
Bug: 152209365
Test: NNT_static
Change-Id: I1facb2dad345958c3b9b1bab4a9564085c382c4a
Merged-In: I1facb2dad345958c3b9b1bab4a9564085c382c4a
(cherry picked from commit b277b676c319f02b44ef91b330593aac591f4fd3)
diff --git a/runtime/Memory.cpp b/runtime/Memory.cpp
index d9a29d4..7bfaf55 100644
--- a/runtime/Memory.cpp
+++ b/runtime/Memory.cpp
@@ -478,6 +478,8 @@
const auto* cb = std::get<const CompilationBuilder*>(role);
return cb->createdWithExplicitDeviceList();
});
+ const uint32_t size = TypeManager::get()->getSizeOfData(mOperand->type, mDesc.dimensions);
+ mShouldFallback &= (size != 0);
mFinished = true;
return ANEURALNETWORKS_NO_ERROR;
}
@@ -488,17 +490,9 @@
return {ANEURALNETWORKS_BAD_STATE, nullptr};
}
- // TODO(xusongw): Does not support dynamic output shape for now.
- CHECK(mOperand.has_value());
- uint32_t size = TypeManager::get()->getSizeOfData(mOperand->type, mDesc.dimensions);
- if (size == 0) {
- LOG(ERROR)
- << "ANeuralNetworksMemory_createFromDesc -- does not support unknown dimensions.";
- return {ANEURALNETWORKS_OP_FAILED, nullptr};
- }
-
int n = ANEURALNETWORKS_OP_FAILED;
std::unique_ptr<Memory> memory;
+ CHECK(mOperand.has_value());
// Try allocate the memory on device.
if (mAllocator != nullptr) {
@@ -507,6 +501,7 @@
// If failed, fallback to ashmem or BLOB mode AHWB.
if (n != ANEURALNETWORKS_NO_ERROR && mShouldFallback) {
+ const uint32_t size = TypeManager::get()->getSizeOfData(mOperand->type, mDesc.dimensions);
if (mSupportsAhwb) {
VLOG(MEMORY) << "MemoryBuilder::allocate -- fallback to BLOB mode AHWB.";
std::tie(n, memory) = MemoryRuntimeAHWB::create(size);
@@ -661,11 +656,11 @@
uint32_t token) {
if (buffer == nullptr) {
LOG(ERROR) << "nullptr IBuffer for device memory.";
- return {ANEURALNETWORKS_BAD_DATA, nullptr};
+ return {ANEURALNETWORKS_OP_FAILED, nullptr};
}
if (token <= 0) {
LOG(ERROR) << "Invalid token for device memory: " << token;
- return {ANEURALNETWORKS_BAD_DATA, nullptr};
+ return {ANEURALNETWORKS_OP_FAILED, nullptr};
}
return {ANEURALNETWORKS_NO_ERROR, std::make_unique<MemoryFromDevice>(std::move(buffer), token)};
};