-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
import span to speed up #68
base: cppflow2
Are you sure you want to change the base?
Conversation
https://en.cppreference.com/w/cpp/language/extending_std I don't think defining With that being said, I do agree to avoid data copy to improve performance. One way is to define |
Yes, you are right. std::span defined in c++20. Maybe we can use the standard span if using compiler supported c++20. |
https://en.cppreference.com/w/cpp/utility/feature_test
https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html
Maybe check __cpp_lib_span, and use std::span if it is available, otherwise
use the self defined version.
…On Wed, Nov 4, 2020, 00:33 Allen ***@***.***> wrote:
Yes, you are right. std::span defined in c++20. Maybe we can use the
standard span if using compiler supported c++20.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#68 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AALDTHSCRT3UCRFAXPJQ7D3SODRSRANCNFSM4TJQ7XVA>
.
|
Okay, I have updated my code to support c++20 feature :) |
|
||
#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}" Threads::Threads) | ||
target_link_libraries (example "${TENSORFLOW_LIB}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep Threads::Threads
. This does not compile on LInux because pthread
is required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you revert all changes to this file? It makes this commit cleaner and focuses on the span header. You can create separate testing files to show it works with and without C++20.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
target_link_libraries (example "${TENSORFLOW_LIB}") is required because it reports link error on my macos.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is your cmake version? Threads::Threads
was introduced in cmake 3.1, and cmake 3.10 is requested here. This specific test case is related to threading, so Threads::Threads
has to be linked.
I strongly recommend creating a separate test case for the span class and do not touch unrelated test case.
include/cppflow/span.h
Outdated
} // end namespace std | ||
|
||
#ifdef __has_cpp_attribute | ||
# if __has_cpp_attribute(__cpp_lib_span) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this will work. According to https://en.cppreference.com/w/cpp/feature_test, "Library features" section:
The following macros are defined if the header <version> or any of the corresponding headers in the table below is included.
which means __cpp_lib_span
is only defined if <span>
or <version>
is included. We need to test __has_include(<span>)
and include it before testing __cpp_lib_span
. There is example code in the bottom of the previous link.
|
||
#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}" Threads::Threads) | ||
target_link_libraries (example "${TENSORFLOW_LIB}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you revert all changes to this file? It makes this commit cleaner and focuses on the span header. You can create separate testing files to show it works with and without C++20.
ee71c98
to
1bafe0f
Compare
Add test for span in c++17 and c++20 and test passed. |
examples/tensor_span/main.cpp
Outdated
@@ -0,0 +1,25 @@ | |||
#include <thread> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you can remove <thread>
You're really serious. LOL. I got an `std::runtime_error' undefined when i remove . Fixed it by the way. |
include/cppflow/tensor.h
Outdated
@@ -205,7 +206,7 @@ namespace cppflow { | |||
|
|||
// Convert to correct type | |||
const auto T_data = static_cast<T*>(raw_data); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have not tested it yet, but res_tensor
is a temporary variable. Returning a data pointer from a temporary variable is alarming for me. Can we use this->tf_tensor
to hold the resolved concrete tensor? Does it deallocate the original tf_tensor and corrupt the data if this->tfe_handle
is created from it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, you are right! It indeed confuses people. Actually, res_tensor
and this->tf_tensor
hold the same buffer, but sometimes res_tensor uses a new buffer.
I think we should use the raw data of this->tf_tensor but not res_tensor.
LOL. Thanks for the fix BTW. I am really looking forward to using this lib in my code, so I just want to make it pretty and clean. Thanks for bearing with me. We still need to look at the behavior of |
I guess I need to go through the source code to see how TF manages memory
internally. My first speculation is that TF_Tensor is RefCounted, so we
should be safe to delete it after obtaining an eager handle. (pure
speculation)
…On Thu, Nov 5, 2020, 2:52 AM Allen ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In include/cppflow/tensor.h
<#68 (comment)>:
> @@ -205,7 +206,7 @@ namespace cppflow {
// Convert to correct type
const auto T_data = static_cast<T*>(raw_data);
Yeah, you are right! It indeed confuses people. Actually, res_tensor and
this->tf_tensor hold the same buffer, but sometimes res_tensor uses a new
buffer.
I think we should use the raw data of this->tf_tensor but not res_tensor.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#68 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AALDTHXJAUCQZ6CGTWNBJ4DSOJKSHANCNFSM4TJQ7XVA>
.
|
I looked tensorflow source:
So the result is up to dtype. |
Maybe it leads to a memory leak. |
Yes, I agree |
I think it is safe to store the resolved What happens under the hood is the following. (2) When we call @ivanallen For |
I use AddressSanitizer detected some memory leak and fixed it. The method get_data really has a memory leak. But we can directly use this->tf_tensor obtaining raw data. It should be safe and no memory leak. |
@ivanallen I noticed the leak messages with gcc's sanitizer before but did not get the time to trace it down. I think the bug fix should be in a separate PR for easier testing. We cannot get data from What I meant was to change |
You know, the |
But we really don't have other choices to keep its ownership, and resolving
the tensor does change the underlying data, though not the pointer itself.
An alternative is to return std::span<const T>.
…On Sat, Nov 7, 2020, 02:09 Allen ***@***.***> wrote:
You know, the get_data method's type is const-qualified, so modify
this->tf_tensor seems unreasonable unless you redesign the method sematic
to non-const or letthis->tf_tensor mutable.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#68 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AALDTHUZTMRPW3EBKGV6BPDSOTXDFANCNFSM4TJQ7XVA>
.
|
@ivanallen Why is this closed? I think it is still worth discussing at least. @serizba I would also like to know your opinion on this before I started to test it. |
sorry, I just update cppflow2 in my github. I will reopen this PR. |
Sorry for not attending this PR before. Regarding The PR is interesting, so no need for closing it :) |
We are adding a standard compatible
We were discussing converting |
@ivanallen Finally, we are ready to work on this PR :) First, could you clean up this PR a bit? Include the following:
After that, I may push some tests to your branch. |
Great! I have rebased my code. |
@ivanallen Thanks a lot, but I think we have more changes to make...
I believe you can also mark all the previous comments as resolved because most of them will be outdated. |
The objective of this PR is to avoid copying the data when retrieving the data with get_data. But, isn't #202 a simpler way of achieving this, and thus avoiding also the inconveniences of std::vector and std::span. Besides, if you need a std::span later on, you can also use the obtained raw_data for this. What do you think? |
In my opinion, they are just two different styles of doing it, ie. the C style and the C++ style. The efficiency/convenience of getting the raw data has improved a lot since this PR, so I don't think its priority is as high as before. In addition, the key issue of this PR is importing a third party |
Yes, that's also my impression. Perhaps for the moment I will include this with a precompiler directive when the C++20 is enabled. And I won't include the span implementation. |
Data copy is bad for performance. I import span(c++20) for speeding up data access.