File tree 5 files changed +47
-3
lines changed
5 files changed +47
-3
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,9 @@ project(example)
4
4
find_library (TENSORFLOW_LIB tensorflow HINT $ENV{HOME} /libtensorflow2/lib)
5
5
6
6
set (CMAKE_CXX_STANDARD 17)
7
+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=address" )
8
+ set (CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -lasan" )
7
9
8
10
add_executable (example main.cpp)
9
11
target_include_directories (example PRIVATE ../../include $ENV{HOME} /libtensorflow2/include )
10
- target_link_libraries (example "${TENSORFLOW_LIB} " )
12
+ target_link_libraries (example "${TENSORFLOW_LIB} " )
Original file line number Diff line number Diff line change @@ -12,5 +12,10 @@ int main() {
12
12
13
13
std::cout << output << std::endl;
14
14
15
+ auto values = output.get_data <float >();
16
+
17
+ for (auto v : values) {
18
+ std::cout << v << std::endl;
19
+ }
15
20
return 0 ;
16
21
}
Original file line number Diff line number Diff line change
1
+ #pragma once
2
+ #include < functional>
3
+
4
+ namespace cppflow {
5
+
6
+ class defer {
7
+ public:
8
+ typedef std::function<void ()> Func;
9
+
10
+ explicit defer (const Func& func) : _func(func) {}
11
+ ~defer () {
12
+ _func ();
13
+ }
14
+
15
+ defer (const defer&) = delete ;
16
+ defer (defer&&) = delete ;
17
+ defer& operator =(const defer&) = delete ;
18
+ void * operator new (size_t ) = delete ;
19
+ void operator delete (void *) = delete ;
20
+
21
+ private:
22
+ Func _func;
23
+ };
24
+
25
+ } // namespace cppflow
Original file line number Diff line number Diff line change 12
12
#include < vector>
13
13
14
14
#include " context.h"
15
+ #include " defer.h"
15
16
#include " tensor.h"
16
17
17
18
namespace cppflow {
@@ -84,6 +85,12 @@ namespace cppflow {
84
85
85
86
std::vector<TF_Output> inp_ops (inputs.size ());
86
87
std::vector<TF_Tensor*> inp_val (inputs.size ());
88
+
89
+ defer d ([&inp_val]{
90
+ for (auto * tf_tensor : inp_val) {
91
+ TF_DeleteTensor (tf_tensor);
92
+ }
93
+ });
87
94
for (int i=0 ; i<inputs.size (); i++) {
88
95
89
96
// Operations
Original file line number Diff line number Diff line change @@ -167,14 +167,16 @@ namespace cppflow {
167
167
168
168
// EXECUTE
169
169
int n = 1 ;
170
- TFE_TensorHandle* res[1 ];
170
+ TFE_TensorHandle* res[1 ] = { nullptr } ;
171
171
TFE_Execute (op, res, &n, context::get_status ());
172
172
status_check (context::get_status ());
173
173
TFE_DeleteOp (op);
174
174
175
175
tensor r;
176
176
r.tf_tensor = { TFE_TensorHandleResolve (res[0 ], context::get_status ()), TF_DeleteTensor};
177
177
status_check (context::get_status ());
178
+ TFE_DeleteTensorHandle (res[0 ]);
179
+
178
180
r.tfe_handle = {TFE_NewTensorHandle (r.tf_tensor .get (), context::get_status ()), TFE_DeleteTensorHandle};
179
181
status_check (context::get_status ());
180
182
@@ -205,7 +207,10 @@ namespace cppflow {
205
207
206
208
// Convert to correct type
207
209
const auto T_data = static_cast <T*>(raw_data);
208
- return std::vector<T>(T_data, T_data + size);
210
+ std::vector<T> r (T_data, T_data + size);
211
+ TF_DeleteTensor (res_tensor);
212
+
213
+ return r;
209
214
}
210
215
211
216
datatype tensor::dtype () const {
You can’t perform that action at this time.
0 commit comments