Skip to content

Commit a4adb8e

Browse files
committed
Merge PR espressif#104 from upstream
2 parents 7722147 + a58644f commit a4adb8e

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ We keep track with the ESP-IDF's support period policy mentioned [here](https://
1818

1919
Currently ESP-IDF versions `release/v4.4` and above are supported by this project.
2020

21+
## Method 1: Using ESP IDF
22+
2123
### Install the ESP IDF
2224

2325
Follow the instructions of the
@@ -29,6 +31,28 @@ The next steps assume that this installation is successful and the
2931
* the `IDF_PATH` environment variable is set
3032
* the `idf.py` and Xtensa-esp32 tools (e.g., `xtensa-esp32-elf-gcc`) are in `$PATH`
3133

34+
## Method 2: Using PlatformIO
35+
36+
### Configuration
37+
38+
Add the following configuration to your `platformio.ini` file:
39+
40+
```ini
41+
[env:your_env_name]
42+
build_flags =
43+
-I ${PROJECT_LIBDEPS_DIR}/${PIOENV}/esp-tflite-micro/third_party/flatbuffers/include
44+
-I ${PROJECT_LIBDEPS_DIR}/${PIOENV}/esp-tflite-micro/third_party/gemmlowp
45+
-I ${PROJECT_LIBDEPS_DIR}/${PIOENV}/esp-tflite-micro/third_party/kissfft
46+
-I ${PROJECT_LIBDEPS_DIR}/${PIOENV}/esp-tflite-micro/third_party/ruy
47+
-Ofast
48+
49+
lib_deps =
50+
https://github.com/espressif/esp-tflite-micro.git
51+
52+
; src_dir =
53+
-I ${PROJECT_LIBDEPS_DIR}/${PIOENV}/esp-tflite-micro/examples/hello_world
54+
```
55+
3256
## Using the component
3357

3458
Run the following command in your ESP-IDF project to install this component:

tensorflow/lite/array.h

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/* Copyright 2023 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_ARRAY_H_
16+
#define TENSORFLOW_LITE_ARRAY_H_
17+
18+
#include <cstring>
19+
#include <initializer_list>
20+
#include <memory>
21+
#include <type_traits>
22+
#include <vector>
23+
24+
#include "tensorflow/lite/core/c/common.h"
25+
26+
namespace tflite {
27+
28+
/// TfLite*Array helpers
29+
30+
namespace array_internal {
31+
32+
// Function object used as a deleter for unique_ptr holding TFLite*Array
33+
// objects.
34+
struct TfLiteArrayDeleter {
35+
void operator()(TfLiteIntArray* a);
36+
void operator()(TfLiteFloatArray* a);
37+
};
38+
39+
// Maps T to the corresponding TfLiteArray type.
40+
template <class T>
41+
struct TfLiteArrayInfo;
42+
43+
template <>
44+
struct TfLiteArrayInfo<int> {
45+
using Type = TfLiteIntArray;
46+
};
47+
48+
template <>
49+
struct TfLiteArrayInfo<float> {
50+
using Type = TfLiteFloatArray;
51+
};
52+
53+
} // namespace array_internal
54+
55+
template <class T>
56+
using TfLiteArrayUniquePtr =
57+
std::unique_ptr<typename array_internal::TfLiteArrayInfo<T>::Type,
58+
array_internal::TfLiteArrayDeleter>;
59+
60+
// `unique_ptr` wrapper for `TfLiteIntArray`s.
61+
using IntArrayUniquePtr = TfLiteArrayUniquePtr<int>;
62+
63+
// `unique_ptr` wrapper for `TfLiteFloatArray`s.
64+
using FloatArrayUniquePtr = TfLiteArrayUniquePtr<float>;
65+
66+
// Allocates a TfLiteArray of given size using malloc.
67+
//
68+
// This builds an int array by default as this is the overwhelming part of the
69+
// use cases.
70+
template <class T = int>
71+
TfLiteArrayUniquePtr<T> BuildTfLiteArray(int size);
72+
73+
// Allocates a TfLiteIntArray of given size using malloc.
74+
template <>
75+
inline IntArrayUniquePtr BuildTfLiteArray<int>(const int size) {
76+
return IntArrayUniquePtr(TfLiteIntArrayCreate(size));
77+
}
78+
79+
// Allocates a TfLiteFloatArray of given size using malloc.
80+
template <>
81+
inline FloatArrayUniquePtr BuildTfLiteArray<float>(const int size) {
82+
return FloatArrayUniquePtr(TfLiteFloatArrayCreate(size));
83+
}
84+
85+
// Allocates a TFLiteArray of given size and initializes it.
86+
//
87+
// `values` is expected to holds `size` elements.
88+
template <class T>
89+
TfLiteArrayUniquePtr<T> BuildTfLiteArray(const int size,
90+
const T* const values) {
91+
auto array = BuildTfLiteArray<T>(size);
92+
if (array) {
93+
memcpy(array->data, values, size * sizeof(T));
94+
}
95+
return array;
96+
}
97+
98+
// Allocates a TFLiteArray and initializes it with the given values.
99+
template <class T>
100+
TfLiteArrayUniquePtr<T> BuildTfLiteArray(const std::vector<T>& values) {
101+
return BuildTfLiteArray(static_cast<int>(values.size()), values.data());
102+
}
103+
104+
// Allocates a TFLiteArray and initializes it with the given values.
105+
template <class T>
106+
TfLiteArrayUniquePtr<T> BuildTfLiteArray(
107+
const std::initializer_list<T>& values) {
108+
return BuildTfLiteArray(static_cast<int>(values.size()), values.begin());
109+
}
110+
111+
// Allocates a TFLiteArray and initializes it with the given array.
112+
inline IntArrayUniquePtr BuildTfLiteArray(const TfLiteIntArray& other) {
113+
return BuildTfLiteArray(other.size, other.data);
114+
}
115+
116+
// Allocates a TFLiteArray and initializes it with the given array.
117+
inline FloatArrayUniquePtr BuildTfLiteArray(const TfLiteFloatArray& other) {
118+
return BuildTfLiteArray(other.size, other.data);
119+
}
120+
121+
} // namespace tflite
122+
123+
#endif // TENSORFLOW_LITE_ARRAY_H_

0 commit comments

Comments
 (0)