Skip to content

Commit

Permalink
import span to speed up & fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanallen authored and liufeng27 committed Nov 6, 2020
1 parent 8db5b32 commit eb6244b
Show file tree
Hide file tree
Showing 8 changed files with 680 additions and 15 deletions.
2 changes: 2 additions & 0 deletions examples/eager_op_multithread/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ find_package(Threads REQUIRED)

set(CMAKE_CXX_STANDARD 17)


#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=thread")
#set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=thread")

add_executable(example main.cpp)
target_include_directories(example PRIVATE ../../include $ENV{HOME}/libtensorflow2/include)
target_link_libraries (example "${TENSORFLOW_LIB}")
target_link_libraries(example "${TENSORFLOW_LIB}" Threads::Threads)
16 changes: 16 additions & 0 deletions examples/tensor_span/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.10)
project(example)

find_library(TENSORFLOW_LIB tensorflow HINT $ENV{HOME}/libtensorflow2/lib)
find_package(Threads REQUIRED)

#set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD 17)


set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -lasan")

add_executable(example main.cpp)
target_include_directories(example PRIVATE ../../include $ENV{HOME}/libtensorflow2/include)
target_link_libraries (example "${TENSORFLOW_LIB}")
23 changes: 23 additions & 0 deletions examples/tensor_span/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <cmath>
#include <vector>
#include <cassert>

#include "cppflow/cppflow.h"

template<typename T>
void test(const std::initializer_list<T>& input) {
std::vector<T> values = input;
cppflow::tensor t(input);
auto span_result = t.get_data<T>();

assert(span_result.size() == values.size());
for(size_t i = 0; i < span_result.size(); i++) {
assert(std::abs(values[i]/span_result[i]-1.0f) < 1e-6);
}
}

int main() {
test({10, 20, 30});
test({10.0, 20.1, 30.3});
return 0;
}
1 change: 1 addition & 0 deletions include/cppflow/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define CPPFLOW2_CONTEXT_H

#include <memory>
#include <stdexcept>
#include <utility>

#include <tensorflow/c/c_api.h>
Expand Down
26 changes: 26 additions & 0 deletions include/cppflow/defer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include <functional>

namespace cppflow {

class defer {
public:
typedef std::function<void ()> Func;

explicit defer(const Func& func) : _func(func) {}
~defer() {
// maybe throw exception
_func();
}

defer(const defer&) = delete;
defer(defer&&) = delete;
defer& operator=(const defer&) = delete;
void* operator new (size_t) = delete;
void operator delete (void*) = delete;

private:
Func _func;
};

} // namespace cppflow
7 changes: 7 additions & 0 deletions include/cppflow/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "context.h"
#include "tensor.h"
#include "defer.h"

namespace cppflow {

Expand Down Expand Up @@ -84,6 +85,12 @@ namespace cppflow {

std::vector<TF_Output> inp_ops(inputs.size());
std::vector<TF_Tensor*> inp_val(inputs.size());

defer d([&inp_val]{
for (auto* tf_tensor : inp_val) {
TF_DeleteTensor(tf_tensor);
}
});
for (int i=0; i<inputs.size(); i++) {

// Operations
Expand Down
Loading

0 comments on commit eb6244b

Please sign in to comment.