From f7490beb6807fcc340208315f21302b7f9f622ce Mon Sep 17 00:00:00 2001 From: Johan Gunnarsson Date: Wed, 30 Jul 2025 17:12:26 +0200 Subject: [PATCH] MLBEDSW-10945: Ensure correct TOSA tensor data alignment Some flatbuffer tools are not respecting the force_align attribute. Constant tensor data is not always aligned to 8 bytes even though tosa.fbs has "data: [ubyte] (force_align: 8)". This casues problems when the tensor data is accessed as int64 in Vela. Signed-off-by: Johan Gunnarsson Change-Id: I2e09e0ac8ce8d7e67dc5868bc121f44d1fb851b9 --- ethosu/regor/tosa/tosa_reader.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/ethosu/regor/tosa/tosa_reader.cpp b/ethosu/regor/tosa/tosa_reader.cpp index 1cb7db61..3e403482 100644 --- a/ethosu/regor/tosa/tosa_reader.cpp +++ b/ethosu/regor/tosa/tosa_reader.cpp @@ -19,6 +19,7 @@ #include "tosa_reader.hpp" #include "common/common.hpp" +#include "common/logging.hpp" #include "common/shape.hpp" #include "compiler/attributes.hpp" @@ -398,7 +399,13 @@ void TosaReader::LoadGraphs(const tosaFb::TosaGraph *model, std::listdata(); if ( tensorData && tensorData->size() ) { - buffer = builder->CreateBuffer(tensorData->size(), GraphApi::BufferMapping::Alias, tensorData->Data()); + GraphApi::BufferMapping mapping = GraphApi::BufferMapping::Alias; + if ( reinterpret_cast(tensorData->Data()) % 8 != 0 ) + { + LOG_DEBUG("{} tensor buffer is not 8 byte aligned\n", name); + mapping = GraphApi::BufferMapping::Allocate; + } + buffer = builder->CreateBuffer(tensorData->size(), mapping, tensorData->Data()); builder_assert(buffer, "Failed to create buffer"); } @@ -429,7 +436,13 @@ void TosaReader::LoadGraphs(const tosaFb::TosaGraph *model, std::listdata(); if ( shapeData && shapeData->size() ) { - buffer = builder->CreateBuffer(shapeData->size(), GraphApi::BufferMapping::Alias, shapeData->Data()); + GraphApi::BufferMapping mapping = GraphApi::BufferMapping::Alias; + if ( reinterpret_cast(shapeData->Data()) % 8 != 0 ) + { + LOG_DEBUG("{} tensor buffer is not 8 byte aligned\n", name); + mapping = GraphApi::BufferMapping::Allocate; + } + buffer = builder->CreateBuffer(shapeData->size(), mapping, shapeData->Data()); builder_assert(buffer, "Failed to create buffer"); } -- GitLab