aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiao Wang <miaowang@google.com>2019-04-24 18:08:56 -0700
committerMiao Wang <miaowang@google.com>2019-04-25 09:29:28 -0700
commit8e31a81e2b913710a939a7fcdac8a5a2d236f38b (patch)
treec151d5e8d932d554d6f4118a954ed159160f0120
parent1e96bcd11772439fb54296d6c0dd6ee57ff3ab18 (diff)
downloadtensorflow-8e31a81e2b913710a939a7fcdac8a5a2d236f38b.tar.gz
Make sure the buffers in NNAPI delegate shared memory pool are 16 bytes
aligned. Bug: 130687286 Test: mm Test: atest -a CtsTfliteNnapiDelegateTestCases Change-Id: I514dc59cf797816d3cd62d81fe5657f6ba135b7f
-rw-r--r--tensorflow/lite/delegates/nnapi/nnapi_delegate.cc14
1 files changed, 14 insertions, 0 deletions
diff --git a/tensorflow/lite/delegates/nnapi/nnapi_delegate.cc b/tensorflow/lite/delegates/nnapi/nnapi_delegate.cc
index b656116e767..dd04e49e158 100644
--- a/tensorflow/lite/delegates/nnapi/nnapi_delegate.cc
+++ b/tensorflow/lite/delegates/nnapi/nnapi_delegate.cc
@@ -91,7 +91,16 @@ bool IsHybridOperator(const TfLiteContext* context, int builtin_code,
constexpr int32_t kMinSdkVersionForNNAPI = 27;
constexpr int32_t kMinSdkVersionForNNAPI11 = 28;
constexpr int32_t kMinSdkVersionForNNAPI12 = 29;
+constexpr size_t kDefaultByteAlignmentForNNAPI = 16;
+static size_t getNumPaddingBytes(size_t byte_size) {
+ size_t num_padding_bytes = 0;
+ if (byte_size % kDefaultByteAlignmentForNNAPI) {
+ num_padding_bytes = kDefaultByteAlignmentForNNAPI -
+ (byte_size % kDefaultByteAlignmentForNNAPI);
+ }
+ return num_padding_bytes;
+}
} // namespace
// RAII NN API Model Destructor for use with std::unique_ptr
@@ -1070,6 +1079,7 @@ class NNAPIDelegateKernel {
execution, relative_input_index, nullptr,
nn_input_memory_->get_handle(), input_offset, tensor->bytes));
input_offset += tensor->bytes;
+ input_offset += getNumPaddingBytes(tensor->bytes);
relative_input_index++;
}
}
@@ -1085,6 +1095,7 @@ class NNAPIDelegateKernel {
execution, relative_output_index, nullptr,
nn_output_memory_->get_handle(), output_offset, tensor->bytes));
output_offset += tensor->bytes;
+ output_offset += getNumPaddingBytes(tensor->bytes);
relative_output_index++;
}
@@ -1124,6 +1135,7 @@ class NNAPIDelegateKernel {
memcpy(tensor->data.raw,
nn_output_memory_->get_data_ptr() + output_offset, tensor->bytes);
output_offset += tensor->bytes;
+ output_offset += getNumPaddingBytes(tensor->bytes);
}
return kTfLiteOk;
@@ -1270,6 +1282,7 @@ class NNAPIDelegateKernel {
context->tensors[i].allocation_type != kTfLiteMmapRo) {
inputs.push_back(operand_mapping_.lite_index_to_ann(i));
total_input_byte_size += context->tensors[i].bytes;
+ total_input_byte_size += getNumPaddingBytes(context->tensors[i].bytes);
}
}
@@ -1277,6 +1290,7 @@ class NNAPIDelegateKernel {
for (int i : TfLiteIntArrayView(output_tensors)) {
outputs.push_back(operand_mapping_.lite_index_to_ann(i));
total_output_byte_size += context->tensors[i].bytes;
+ total_output_byte_size += getNumPaddingBytes(context->tensors[i].bytes);
}
// Add state output tensors as model outputs.