Skip to content

Commit 33795b6

Browse files
authored
Align model data and arena to 16 bytes (tensorflow#149)
* Align model data and arena to 16 bytes bug=fixes #148 * fix copyright
1 parent 3817c2a commit 33795b6

File tree

8 files changed

+25078
-23154
lines changed

8 files changed

+25078
-23154
lines changed

examples/hello_world/hello_world.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -34,7 +34,8 @@ TfLiteTensor* output = nullptr;
3434
int inference_count = 0;
3535

3636
constexpr int kTensorArenaSize = 2000;
37-
uint8_t tensor_arena[kTensorArenaSize];
37+
// Keep aligned to 16 bytes for CMSIS
38+
alignas(16) uint8_t tensor_arena[kTensorArenaSize];
3839
} // namespace
3940

4041
// The name of this function is important for Arduino compatibility.

examples/hello_world/model.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -24,8 +24,8 @@ limitations under the License.
2424

2525
#include "model.h"
2626

27-
// Keep model aligned to 8 bytes to guarantee aligned 64-bit accesses.
28-
alignas(8) const unsigned char g_model[] = {
27+
// Keep aligned to 16 bytes for CMSIS
28+
alignas(16) const unsigned char g_model[] = {
2929
0x1c, 0x00, 0x00, 0x00, 0x54, 0x46, 0x4c, 0x33, 0x14, 0x00, 0x20, 0x00,
3030
0x1c, 0x00, 0x18, 0x00, 0x14, 0x00, 0x10, 0x00, 0x0c, 0x00, 0x00, 0x00,
3131
0x08, 0x00, 0x04, 0x00, 0x14, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
@@ -234,4 +234,5 @@ alignas(8) const unsigned char g_model[] = {
234234
0x0c, 0x00, 0x10, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00,
235235
0x0c, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
236236
0x00, 0x00, 0x00, 0x09};
237-
const int g_model_len = 2488;
237+
238+
const int g_model_len = sizeof(g_model);

examples/magic_wand/magic_wand.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
22
Licensed under the Apache License, Version 2.0 (the "License");
33
you may not use this file except in compliance with the License.
44
You may obtain a copy of the License at
@@ -95,7 +95,8 @@ enum {
9595
// The size of this will depend on the model you're using, and may need to be
9696
// determined by experimentation.
9797
constexpr int kTensorArenaSize = 30 * 1024;
98-
uint8_t tensor_arena[kTensorArenaSize];
98+
// Keep aligned to 16 bytes for CMSIS
99+
alignas(16) uint8_t tensor_arena[kTensorArenaSize];
99100

100101
const tflite::Model* model = nullptr;
101102
tflite::MicroInterpreter* interpreter = nullptr;

examples/magic_wand/magic_wand_model_data.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
22
Licensed under the Apache License, Version 2.0 (the "License");
33
you may not use this file except in compliance with the License.
44
You may obtain a copy of the License at
@@ -12,7 +12,8 @@ limitations under the License.
1212

1313
#include "magic_wand_model_data.h"
1414

15-
const unsigned char g_magic_wand_model_data[] = {
15+
// Keep aligned to 16 bytes for CMSIS
16+
alignas(16) const unsigned char g_magic_wand_model_data[] = {
1617
0x20, 0x00, 0x00, 0x00, 0x54, 0x46, 0x4c, 0x33, 0x00, 0x00, 0x00, 0x00,
1718
0x14, 0x00, 0x20, 0x00, 0x04, 0x00, 0x08, 0x00, 0x0c, 0x00, 0x10, 0x00,
1819
0x14, 0x00, 0x00, 0x00, 0x18, 0x00, 0x1c, 0x00, 0x14, 0x00, 0x00, 0x00,

examples/micro_speech/micro_features_model.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -20,19 +20,8 @@ limitations under the License.
2020

2121
#include "micro_features_model.h"
2222

23-
// We need to keep the data array aligned on some architectures.
24-
#ifdef __has_attribute
25-
#define HAVE_ATTRIBUTE(x) __has_attribute(x)
26-
#else
27-
#define HAVE_ATTRIBUTE(x) 0
28-
#endif
29-
#if HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__))
30-
#define DATA_ALIGN_ATTRIBUTE __attribute__((aligned(4)))
31-
#else
32-
#define DATA_ALIGN_ATTRIBUTE
33-
#endif
34-
35-
const unsigned char g_model[] DATA_ALIGN_ATTRIBUTE = {
23+
// Keep aligned to 16 bytes for CMSIS
24+
alignas(16) const unsigned char g_model[] = {
3625
0x20, 0x00, 0x00, 0x00, 0x54, 0x46, 0x4c, 0x33, 0x00, 0x00, 0x00, 0x00,
3726
0x00, 0x00, 0x12, 0x00, 0x1c, 0x00, 0x04, 0x00, 0x08, 0x00, 0x0c, 0x00,
3827
0x10, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x00, 0x12, 0x00, 0x00, 0x00,
@@ -1593,4 +1582,5 @@ const unsigned char g_model[] DATA_ALIGN_ATTRIBUTE = {
15931582
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x07, 0x00,
15941583
0x00, 0x00, 0x08, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
15951584
0x03, 0x00, 0x00, 0x00};
1596-
const int g_model_len = 18712;
1585+
1586+
const int g_model_len = sizeof(g_model);

examples/micro_speech/micro_speech.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -43,7 +43,8 @@ int32_t previous_time = 0;
4343
// The size of this will depend on the model you're using, and may need to be
4444
// determined by experimentation.
4545
constexpr int kTensorArenaSize = 10 * 1024;
46-
uint8_t tensor_arena[kTensorArenaSize];
46+
// Keep aligned to 16 bytes for CMSIS
47+
alignas(16) uint8_t tensor_arena[kTensorArenaSize];
4748
int8_t feature_buffer[kFeatureElementCount];
4849
int8_t* model_input_buffer = nullptr;
4950
} // namespace

0 commit comments

Comments
 (0)