message: Make sure the buffers in NNAPI shared memory pool are 16 bytes aligned.
diff: @@ -12,6 +12,8 @@ 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 "tensorflow/lite/delegates/nnapi/nnapi_delegate.h"
  +
  #include <cstdarg>
  #include <cstring>
  #include <iostream>
  @@ -23,7 +25,6 @@ limitations under the License.
  #include "tensorflow/lite/builtin_ops.h"
  #include "tensorflow/lite/c/c_api_internal.h"
  #include "tensorflow/lite/context_util.h"
  -#include "tensorflow/lite/delegates/nnapi/nnapi_delegate.h"
  #include "tensorflow/lite/kernels/kernel_util.h"
  #include "tensorflow/lite/nnapi/nnapi_implementation.h"
  @@ -116,7 +117,16 @@ bool IsRestrictedScalesCompliant(const TfLiteContext* context,
  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
  @@ -1156,6 +1166,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++;
  }
  }
  @@ -1171,6 +1182,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++;
  }
  @@ -1210,6 +1222,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;
  @@ -1376,6 +1389,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);
  }
  }
  @@ -1383,6 +1397,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.
  