From 5e36ca05e729bfd2a04cf2024e174c4c9b74b751 Mon Sep 17 00:00:00 2001 From: Xusong Wang Date: Tue, 16 Feb 2021 10:40:32 -0800 Subject: [PATCH] Passing padding information to the driver -- hal. This CL changes the sAIDL interface to enable passing padding information of the shared memory pool to the driver. The sAIDL interface defines the padding field explicitly in DataLocation to make it easy to convert to/from the canonical request. Bug: 179691454 Test: NNT_static Test: VtsHalNeuralnetworksTargetTest Change-Id: Ie13b421531ee4df48822086b027d94a622a3518c Merged-In: Ie13b421531ee4df48822086b027d94a622a3518c (cherry picked from commit 6365ea1dbba94da8a0f2fb63d00283109ee47f9b) --- .../hardware/neuralnetworks/DataLocation.aidl | 1 + .../hardware/neuralnetworks/DataLocation.aidl | 21 +++++++++++++++++++ neuralnetworks/aidl/utils/src/Conversions.cpp | 6 ++++++ .../aidl/vts/functional/MemoryDomainTests.cpp | 9 +++++--- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/neuralnetworks/aidl/aidl_api/android.hardware.neuralnetworks/current/android/hardware/neuralnetworks/DataLocation.aidl b/neuralnetworks/aidl/aidl_api/android.hardware.neuralnetworks/current/android/hardware/neuralnetworks/DataLocation.aidl index 074cc0922d..e836daec96 100644 --- a/neuralnetworks/aidl/aidl_api/android.hardware.neuralnetworks/current/android/hardware/neuralnetworks/DataLocation.aidl +++ b/neuralnetworks/aidl/aidl_api/android.hardware.neuralnetworks/current/android/hardware/neuralnetworks/DataLocation.aidl @@ -36,4 +36,5 @@ parcelable DataLocation { int poolIndex; long offset; long length; + long padding; } diff --git a/neuralnetworks/aidl/android/hardware/neuralnetworks/DataLocation.aidl b/neuralnetworks/aidl/android/hardware/neuralnetworks/DataLocation.aidl index f6b5e0d2ac..1b2378f016 100644 --- a/neuralnetworks/aidl/android/hardware/neuralnetworks/DataLocation.aidl +++ b/neuralnetworks/aidl/android/hardware/neuralnetworks/DataLocation.aidl @@ -18,6 +18,23 @@ package android.hardware.neuralnetworks; /** * Describes the location of a data object. + * + * If the data object is an omitted operand, all of the fields must be 0. If the poolIndex refers to + * a driver-managed buffer allocated from IDevice::allocate, or an AHardwareBuffer of a format other + * than AHARDWAREBUFFER_FORMAT_BLOB, the offset, length, and padding must be set to 0 indicating + * the entire pool is used. + * + * Otherwise, the offset, length, and padding specify a sub-region of a memory pool. The sum of + * offset, length, and padding must not exceed the total size of the specified memory pool. If the + * data object is a scalar operand or a tensor operand with fully specified dimensions, the value of + * length must be equal to the raw size of the operand (i.e. the size of an element multiplied + * by the number of elements). When used in Operand, the value of padding must be 0. When used in + * RequestArgument, the value of padding specifies the extra bytes at the end of the memory region + * that may be used by the device to access memory in chunks, for efficiency. If the data object is + * a Request output whose dimensions are not fully specified, the value of length specifies the + * total size of the writable region of the output data, and padding specifies the extra bytes at + * the end of the memory region that may be used by the device to access memory in chunks, for + * efficiency, but must not be used to hold any output data. */ @VintfStability parcelable DataLocation { @@ -33,4 +50,8 @@ parcelable DataLocation { * The length of the data in bytes. */ long length; + /** + * The end padding of the specified memory region in bytes. + */ + long padding; } diff --git a/neuralnetworks/aidl/utils/src/Conversions.cpp b/neuralnetworks/aidl/utils/src/Conversions.cpp index db3504bb74..9a06d475f8 100644 --- a/neuralnetworks/aidl/utils/src/Conversions.cpp +++ b/neuralnetworks/aidl/utils/src/Conversions.cpp @@ -254,16 +254,22 @@ GeneralResult unvalidatedConvert(const aidl_hal::DataLocation& loc VERIFY_NON_NEGATIVE(location.poolIndex) << "DataLocation: pool index must not be negative"; VERIFY_NON_NEGATIVE(location.offset) << "DataLocation: offset must not be negative"; VERIFY_NON_NEGATIVE(location.length) << "DataLocation: length must not be negative"; + VERIFY_NON_NEGATIVE(location.padding) << "DataLocation: padding must not be negative"; if (location.offset > std::numeric_limits::max()) { return NN_ERROR() << "DataLocation: offset must be <= std::numeric_limits::max()"; } if (location.length > std::numeric_limits::max()) { return NN_ERROR() << "DataLocation: length must be <= std::numeric_limits::max()"; } + if (location.padding > std::numeric_limits::max()) { + return NN_ERROR() + << "DataLocation: padding must be <= std::numeric_limits::max()"; + } return DataLocation{ .poolIndex = static_cast(location.poolIndex), .offset = static_cast(location.offset), .length = static_cast(location.length), + .padding = static_cast(location.padding), }; } diff --git a/neuralnetworks/aidl/vts/functional/MemoryDomainTests.cpp b/neuralnetworks/aidl/vts/functional/MemoryDomainTests.cpp index 1929750d28..57bc1aed54 100644 --- a/neuralnetworks/aidl/vts/functional/MemoryDomainTests.cpp +++ b/neuralnetworks/aidl/vts/functional/MemoryDomainTests.cpp @@ -1125,12 +1125,15 @@ TEST_P(MemoryDomainExecutionTest, InvalidDimensions) { utils::toSigned(kTestOperand.dimensions).value()); if (deviceBuffer.buffer == nullptr) return; - RequestMemoryPool sharedMemory = createSharedMemoryPool(kTestOperandDataSize); - RequestMemoryPool deviceMemory = createDeviceMemoryPool(deviceBuffer.token); + // Use an incompatible dimension and make sure the length matches with the bad dimension. auto badDimensions = utils::toSigned(kTestOperand.dimensions).value(); badDimensions[0] = 2; + const uint32_t badTestOperandDataSize = kTestOperandDataSize * 2; + + RequestMemoryPool sharedMemory = createSharedMemoryPool(badTestOperandDataSize); + RequestMemoryPool deviceMemory = createDeviceMemoryPool(deviceBuffer.token); RequestArgument sharedMemoryArg = { - .location = {.poolIndex = 0, .offset = 0, .length = kTestOperandDataSize}, + .location = {.poolIndex = 0, .offset = 0, .length = badTestOperandDataSize}, .dimensions = badDimensions}; RequestArgument deviceMemoryArg = {.location = {.poolIndex = 1}}; RequestArgument deviceMemoryArgWithBadDimensions = {.location = {.poolIndex = 1},