Make sure tensor sizes are 64-byte aligned (#222)

This helps reduce misaligned access violation
This commit is contained in:
KeDengMS 2018-12-19 13:45:04 -08:00 committed by GitHub
parent 4d010fb1ea
commit b9cc134576
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 3 deletions

View file

@ -89,7 +89,7 @@ Status ExecutionFrame::AllocateMLValueTensorSelfOwnBufferHelper(int mlvalue_inde
if (len < 0) {
return Status(ONNXRUNTIME, INVALID_ARGUMENT, "Tensor shape cannot contain any negative value");
}
if (!IAllocator::CalcMemSizeForArray(len, element_type->Size(), &size)) {
if (!IAllocator::CalcMemSizeForArrayWithAlignment<64>(len, element_type->Size(), &size)) {
return Status(ONNXRUNTIME, FAIL, "size overflow");
}
}

View file

@ -257,9 +257,9 @@ TEST(ExecutionFrameTest, MemPatternTest) {
EXPECT_EQ(pattern.patterns.size(), pattern.locations.size());
EXPECT_EQ(pattern.patterns.size(), 1);
auto p = pattern.GetPatterns(cpu_allocator->Info());
EXPECT_EQ(p->PeakSize(), sizeof(float) * (4 + 6));
EXPECT_EQ(p->PeakSize(), 2 * 64); // each allocation is 64-byte aligned
EXPECT_EQ(p->GetBlock(3)->offset_, 0);
EXPECT_EQ(p->GetBlock(4)->offset_, sizeof(float) * 4);
EXPECT_EQ(p->GetBlock(4)->offset_, 64);
}
} // namespace test
} // namespace onnxruntime