import span to speed up - #68
Conversation
ljn917
commented
Nov 4, 2020
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 |
ivanallen
commented
Nov 4, 2020
Yes, you are right. std::span defined in c++20. Maybe we can use the standard span if using compiler supported c++20. |
ljn917
commented
Nov 4, 2020
via email
https://en.cppreference.com/w/cpp/utility/feature_testhttps://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>
.
|
ivanallen
commented
Nov 5, 2020
Okay, I have updated my code to support c++20 feature :) |
| 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.
Please keep Threads::Threads. This does not compile on LInux because pthread is required.
There was a problem hiding this comment.
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.
target_link_libraries (example "${TENSORFLOW_LIB}") is required because it reports link error on my macos.
There was a problem hiding this comment.
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.
| } // end namespace std | ||
| #ifdef __has_cpp_attribute | ||
| # if __has_cpp_attribute(__cpp_lib_span) |
There was a problem hiding this comment.
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.
| 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.
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
1bafe0fCompareivanallen
commented
Nov 5, 2020
Add test for span in c++17 and c++20 and test passed. |
| @@ -0,0 +1,25 @@ | |||
| #include <thread> | |||
ivanallen
commented
Nov 5, 2020
You're really serious. LOL. I got an `std::runtime_error' undefined when i remove . Fixed it by the way. |
| @@ -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.
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.
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.
ljn917
commented
Nov 5, 2020
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 |
ljn917
commented
Nov 5, 2020
via email
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>
.
|
ivanallen
commented
Nov 5, 2020
I looked tensorflow source: So the result is up to dtype. |
ivanallen
commented
Nov 5, 2020
Maybe it leads to a memory leak. |
ljn917
commented
Nov 5, 2020
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 |
ivanallen
commented
Nov 6, 2020
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 |
ivanallen
commented
Nov 7, 2020
You know, the |
ljn917
commented
Nov 7, 2020
via email
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>
.
|
ljn917
commented
Nov 26, 2020
@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. |
ivanallen
commented
Nov 26, 2020
sorry, I just update cppflow2 in my github. I will reopen this PR. |
serizba
commented
Nov 27, 2020
Sorry for not attending this PR before. Regarding The PR is interesting, so no need for closing it :) |
ljn917
commented
Nov 28, 2020
We are adding a standard compatible
We were discussing converting |
ljn917
commented
Dec 1, 2020
@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. |
ivanallen
commented
Dec 2, 2020
Great! I have rebased my code. |
ljn917
commented
Dec 4, 2020
@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. |
serizba
commented
Sep 23, 2022
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? |
ljn917
commented
Sep 28, 2022
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 |
serizba
commented
Sep 29, 2022
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.