Skip to content

Commit f8bdc07

Browse files
committed
Revert static_vector and make building for c++11
1 parent a3b091f commit f8bdc07

File tree

5 files changed

+199
-5
lines changed

5 files changed

+199
-5
lines changed

tensorflow/lite/micro/BUILD

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,14 @@ tflm_cc_library(
405405
hdrs = ["span.h"],
406406
)
407407

408+
tflm_cc_library(
409+
name = "static_vector",
410+
hdrs = ["static_vector.h"],
411+
deps = [
412+
"//tensorflow/lite/kernels:op_macros",
413+
],
414+
)
415+
408416
tflm_cc_library(
409417
name = "system_setup",
410418
srcs = [
@@ -426,6 +434,7 @@ tflm_cc_library(
426434
deps = [
427435
":micro_log",
428436
":span",
437+
":static_vector",
429438
],
430439
)
431440

@@ -639,6 +648,18 @@ tflm_cc_test(
639648
],
640649
)
641650

651+
tflm_cc_test(
652+
name = "static_vector_test",
653+
size = "small",
654+
srcs = [
655+
"static_vector_test.cc",
656+
],
657+
deps = [
658+
":static_vector",
659+
"//tensorflow/lite/micro/testing:micro_test",
660+
],
661+
)
662+
642663
tflm_cc_test(
643664
name = "hexdump_test",
644665
size = "small",

tensorflow/lite/micro/static_vector.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2024 The TensorFlow Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef TENSORFLOW_LITE_MICRO_STATIC_VECTOR_H_
16+
#define TENSORFLOW_LITE_MICRO_STATIC_VECTOR_H_
17+
18+
#include <array>
19+
#include <cassert>
20+
#include <cstddef>
21+
22+
#include "tensorflow/lite/kernels/op_macros.h" // for TF_LITE_ASSERT
23+
24+
namespace tflite {
25+
26+
template <typename T, std::size_t MaxSize>
27+
class StaticVector {
28+
// A staticlly-allocated vector. Add to the interface as needed.
29+
30+
private:
31+
std::array<T, MaxSize> array_;
32+
std::size_t size_{0};
33+
34+
public:
35+
using iterator = typename decltype(array_)::iterator;
36+
using const_iterator = typename decltype(array_)::const_iterator;
37+
using pointer = typename decltype(array_)::pointer;
38+
using reference = typename decltype(array_)::reference;
39+
using const_reference = typename decltype(array_)::const_reference;
40+
41+
StaticVector() {}
42+
43+
StaticVector(std::initializer_list<T> values) {
44+
for (const T& v : values) {
45+
push_back(v);
46+
}
47+
}
48+
49+
static constexpr std::size_t max_size() { return MaxSize; }
50+
std::size_t size() const { return size_; }
51+
bool full() const { return size() == max_size(); }
52+
iterator begin() { return array_.begin(); }
53+
const_iterator begin() const { return array_.begin(); }
54+
iterator end() { return begin() + size(); }
55+
const_iterator end() const { return begin() + size(); }
56+
pointer data() { return array_.data(); }
57+
reference operator[](int i) { return array_[i]; }
58+
const_reference operator[](int i) const { return array_[i]; }
59+
void clear() { size_ = 0; }
60+
61+
template <std::size_t N>
62+
bool operator==(const StaticVector<T, N>& other) const {
63+
return std::equal(begin(), end(), other.begin(), other.end());
64+
}
65+
66+
template <std::size_t N>
67+
bool operator!=(const StaticVector<T, N>& other) const {
68+
return !(*this == other);
69+
}
70+
71+
void push_back(const T& t) {
72+
TF_LITE_ASSERT(!full());
73+
*end() = t;
74+
++size_;
75+
}
76+
};
77+
78+
template <typename T, typename... U>
79+
StaticVector(T, U...) -> StaticVector<T, 1 + sizeof...(U)>;
80+
81+
} // end namespace tflite
82+
83+
#endif // TENSORFLOW_LITE_MICRO_STATIC_VECTOR_H_
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2024 The TensorFlow Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "tensorflow/lite/micro/static_vector.h"
16+
17+
#include "tensorflow/lite/micro/testing/micro_test.h"
18+
19+
using tflite::StaticVector;
20+
21+
TF_LITE_MICRO_TESTS_BEGIN
22+
23+
TF_LITE_MICRO_TEST(StaticVectorPushBack) {
24+
StaticVector<int, 4> a;
25+
TF_LITE_MICRO_EXPECT(a.max_size() == 4);
26+
TF_LITE_MICRO_EXPECT(a.size() == 0);
27+
28+
a.push_back(1);
29+
TF_LITE_MICRO_EXPECT(a.size() == 1);
30+
TF_LITE_MICRO_EXPECT(a[0] == 1);
31+
32+
a.push_back(2);
33+
TF_LITE_MICRO_EXPECT(a.size() == 2);
34+
TF_LITE_MICRO_EXPECT(a[1] == 2);
35+
36+
a.push_back(3);
37+
TF_LITE_MICRO_EXPECT(a.size() == 3);
38+
TF_LITE_MICRO_EXPECT(a[2] == 3);
39+
}
40+
41+
TF_LITE_MICRO_TEST(StaticVectorInitializationPartial) {
42+
const StaticVector<int, 4> a{1, 2, 3};
43+
TF_LITE_MICRO_EXPECT(a.max_size() == 4);
44+
TF_LITE_MICRO_EXPECT(a.size() == 3);
45+
TF_LITE_MICRO_EXPECT(a[0] == 1);
46+
TF_LITE_MICRO_EXPECT(a[1] == 2);
47+
TF_LITE_MICRO_EXPECT(a[2] == 3);
48+
}
49+
50+
TF_LITE_MICRO_TEST(StaticVectorInitializationFull) {
51+
const StaticVector b{1, 2, 3};
52+
TF_LITE_MICRO_EXPECT(b.max_size() == 3);
53+
TF_LITE_MICRO_EXPECT(b.size() == 3);
54+
}
55+
56+
TF_LITE_MICRO_TEST(StaticVectorEquality) {
57+
const StaticVector a{1, 2, 3};
58+
const StaticVector b{1, 2, 3};
59+
TF_LITE_MICRO_EXPECT(a == b);
60+
TF_LITE_MICRO_EXPECT(!(a != b));
61+
}
62+
63+
TF_LITE_MICRO_TEST(StaticVectorInequality) {
64+
const StaticVector a{1, 2, 3};
65+
const StaticVector b{3, 2, 1};
66+
TF_LITE_MICRO_EXPECT(a != b);
67+
TF_LITE_MICRO_EXPECT(!(a == b));
68+
}
69+
70+
TF_LITE_MICRO_TEST(StaticVectorSizeInequality) {
71+
const StaticVector a{1, 2};
72+
const StaticVector b{1, 2, 3};
73+
TF_LITE_MICRO_EXPECT(a != b);
74+
}
75+
76+
TF_LITE_MICRO_TEST(StaticVectorPartialSizeInequality) {
77+
const StaticVector<int, 3> a{1, 2};
78+
const StaticVector<int, 3> b{1, 2, 3};
79+
TF_LITE_MICRO_EXPECT(a != b);
80+
}
81+
82+
TF_LITE_MICRO_TESTS_END

tensorflow/lite/micro/tools/make/Makefile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ ifeq ($(TARGET), $(HOST_OS))
183183
endif
184184

185185
CXXFLAGS := \
186-
-std=c++11 \
187186
-fno-rtti \
188187
-fno-exceptions \
189188
-fno-threadsafe-statics \
@@ -192,7 +191,6 @@ CXXFLAGS := \
192191

193192
CCFLAGS := \
194193
-Wimplicit-function-declaration \
195-
-std=c11 \
196194
$(COMMON_FLAGS)
197195

198196
ARFLAGS := -r
@@ -258,10 +256,14 @@ else ifeq ($(BUILD_TYPE), no_tf_lite_static_memory)
258256
# https://github.com/tensorflow/tensorflow/issues/43076
259257
CXXFLAGS := $(filter-out -DTF_LITE_STATIC_MEMORY, $(CXXFLAGS))
260258
CCFLAGS := $(filter-out -DTF_LITE_STATIC_MEMORY, $(CCFLAGS))
259+
endif
261260

262-
# array.h uses std::conditional_t, which is a C++14 feature
263-
CXXFLAGS := $(filter-out -std=c++11, $(CXXFLAGS))
264-
CXXFLAGS += -std=c++14
261+
ifeq ($(CC_VER11), true)
262+
CXXFLAGS+=-std=c++11
263+
CCFLAGS+=-std=c11
264+
else
265+
CXXFLAGS+=-std=c++17
266+
CCFLAGS+=-std=c17
265267
endif
266268

267269
# This library is the main target for this makefile. It will contain a minimal
@@ -351,6 +353,7 @@ $(TENSORFLOW_ROOT)tensorflow/lite/micro/micro_time_test.cc \
351353
$(TENSORFLOW_ROOT)tensorflow/lite/micro/micro_utils_test.cc \
352354
$(TENSORFLOW_ROOT)tensorflow/lite/micro/recording_micro_allocator_test.cc \
353355
$(TENSORFLOW_ROOT)tensorflow/lite/micro/span_test.cc \
356+
$(TENSORFLOW_ROOT)tensorflow/lite/micro/static_vector_test.cc \
354357
$(TENSORFLOW_ROOT)tensorflow/lite/micro/arena_allocator/non_persistent_arena_buffer_allocator_test.cc \
355358
$(TENSORFLOW_ROOT)tensorflow/lite/micro/arena_allocator/persistent_arena_buffer_allocator_test.cc \
356359
$(TENSORFLOW_ROOT)tensorflow/lite/micro/arena_allocator/recording_single_arena_buffer_allocator_test.cc \

tensorflow/lite/micro/tools/make/targets/xtensa_makefile.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ TARGET_TOOLCHAIN_PREFIX := xt-
4747
CXX_TOOL := clang++
4848
CC_TOOL := clang
4949

50+
# Building with C++17 requires libc++
51+
ifneq ($(CC_VER11), true)
52+
PLATFORM_FLAGS += -stdlib=libc++
53+
endif
54+
5055
# Unused exception related symbols make their way into a binary that links
5156
# against TFLM as described in https://github.com/tensorflow/tensorflow/issues/47575.
5257
# We have two options to avoid this. The first involves using -stdlib=libc++ and

0 commit comments

Comments
 (0)