-
Notifications
You must be signed in to change notification settings - Fork 22.6k
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
Fix TORCH_LIBRARIES variables when do static build #49458
Conversation
💊 CI failures summary and remediationsAs of commit 1b2e102 (more details on the Dr. CI page):
🕵️ 3 new failures recognized by patternsThe following CI failures do not appear to be due to upstream breakages: pytorch_linux_xenial_py3_6_gcc5_4_build (1/3)Step: "(Optional) Merge target branch" (full log | diagnosis details | 🔁 rerun)
|
@malfet could you have a code review for this PR? |
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.
Overall LGTM, but please address review feedback, namely refactor repeated patterns into append_wholearchive_lib_if_found
cmake/TorchConfig.cmake.in
Outdated
find_library(C10_LIBRARY c10 PATHS "${TORCH_INSTALL_PREFIX}/lib") | ||
list(APPEND TORCH_LIBRARIES ${C10_LIBRARY}) |
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.
Why not use append_torchlib
macro here?
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 didn't plan to touch the code which belongs to shared library build. Anyway, I can fix this if it looks good.
cmake/TorchConfig.cmake.in
Outdated
if(${_arg}_LIBRARY) | ||
list(APPEND TORCH_LIBRARIES ${${_arg}_LIBRARY}) | ||
else() | ||
message(WARNING "static library ${${_arg}_LIBRARY} not found.") |
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 explain why this one should not be an error?
Also, why this macro is specific to a static libraries discovery?
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.
warning instead of error mainly because of two reasons:
1, I cannot find the corresponding CMake option for every individual static library, so if the static library is not found, means the implied cmake option is off (rather than an error);
2, some static libraries are not mandatory for end user, warning instead of error provides flexibility in this situation.
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.
Hmm, can you give an example of optional library dependencies?
Also "static library" message is misleading, as append_torchlib_if_found
is called from both shared and static library path.
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.
eigen_blas is optional, as we may use MKL as lapack backend, and I cannot find a public cmake option to control this,e.g.:
if(@USE_EIGENBLAS@)
If eigen_blas option can be found, and also caffe2_protos, protobuf-lite, protobuf, protoc, onnx, onnx_proto, foxi_loader, fmt, sleef, asmjit are all mandatory library for static build, we can change the warning to error.
cmake/TorchConfig.cmake.in
Outdated
endforeach() | ||
endmacro() | ||
|
||
function(add_whole_archive_flag lib output_var) |
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 please explain why add_whole_archive_flag
is a function, but append_torchlib_if_found
is macro?
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.
list append seems not support parent scope, while "set list" seems not intuitive, so use macro instead. And, to be consistent, I will change the function(add_whole_archive_flag) to macro if it looks good.
cmake/TorchConfig.cmake.in
Outdated
else() | ||
set(library_with_flag "") | ||
endif() |
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 please explain, why we shouldn't raise an error if torch_LIBRARY could not be found?
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.
Some static libraries are optional, e.g. for torch_cuda and c10_cuda. After pytorch cuda build, end user can still link libtorch with cuda libraries missing (just use the cpu device) .
cmake/TorchConfig.cmake.in
Outdated
add_whole_archive_flag(${torch_cpu_LIBRARY} library_with_flag) | ||
else() | ||
set(library_with_flag "") | ||
endif() | ||
list(APPEND TORCH_LIBRARIES ${library_with_flag}) |
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 please explain why else() statement is needed?
add_whole_archive_flag(${torch_cpu_LIBRARY} library_with_flag) | |
else() | |
set(library_with_flag "") | |
endif() | |
list(APPEND TORCH_LIBRARIES ${library_with_flag}) | |
add_whole_archive_flag(${torch_cpu_LIBRARY} library_with_flag) | |
list(APPEND TORCH_LIBRARIES ${library_with_flag}) | |
endif() |
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.
You are right.
cmake/TorchConfig.cmake.in
Outdated
if(torch_cuda_LIBRARY) | ||
add_whole_archive_flag(${torch_cuda_LIBRARY} library_with_flag) | ||
else() | ||
set(library_with_flag "") | ||
endif() | ||
list(APPEND TORCH_LIBRARIES ${library_with_flag}) |
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.
This looks like a repeated pattern, can it be refactored to macro? Something like add_wholearchive_library_if_found
?
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.
yes, this will be done.
@malfet All have been 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.
@malfet has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
Fixes #21737
With this fix, TORCH_LIBRARIES variable can provide all nessesary static libraries build from pytorch repo.
User program (if do static build) now can just link with ${TORCH_LIBRARIES} + MKL + cuda runtime.