Skip to content

Commit

Permalink
Resolving native dependency and try_run for cross compile (pytorch#59764
Browse files Browse the repository at this point in the history
)

Summary:
This is a PR on build system that provides support for cross compiling on Jetson platforms.

The major change is:

1. Disable try runs for cross compiling in `COMPILER_WORKS`, `BLAS`, and `CUDA`. They will not be able to perform try run on a cross compile setup

Pull Request resolved: pytorch#59764

Reviewed By: soulitzer

Differential Revision: D29524363

Pulled By: malfet

fbshipit-source-id: f06d1ad30b704c9a17d77db686c65c0754db07b8
  • Loading branch information
shmsong authored and facebook-github-bot committed Jul 9, 2021
1 parent 8bd3e52 commit ee2dd35
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 78 deletions.
12 changes: 9 additions & 3 deletions cmake/MiscCheck.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ if(NOT INTERN_BUILD_MOBILE)
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_OSX_ARCHITECTURES MATCHES "^(x86_64|arm64)$")
list(APPEND CMAKE_REQUIRED_FLAGS "-arch ${CMAKE_HOST_SYSTEM_PROCESSOR}")
endif()
CHECK_C_SOURCE_RUNS("
int main() { return 0; }
" COMPILER_WORKS)
if(CMAKE_CROSSCOMPILING)
CHECK_C_SOURCE_COMPILES("
int main() { return 0; }
" COMPILER_WORKS)
else()
CHECK_C_SOURCE_RUNS("
int main() { return 0; }
" COMPILER_WORKS)
endif()
if(NOT COMPILER_WORKS)
# Force cmake to retest next time around
unset(COMPILER_WORKS CACHE)
Expand Down
116 changes: 66 additions & 50 deletions cmake/Modules/FindBLAS.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -298,56 +298,72 @@ IF (BLAS_LIBRARIES)
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_OSX_ARCHITECTURES MATCHES "^(x86_64|arm64)$")
list(APPEND CMAKE_REQUIRED_FLAGS "-arch ${CMAKE_HOST_SYSTEM_PROCESSOR}")
endif()
SET(CMAKE_REQUIRED_LIBRARIES ${BLAS_LIBRARIES})
CHECK_C_SOURCE_RUNS("
#include <stdlib.h>
#include <stdio.h>
float x[4] = { 1, 2, 3, 4 };
float y[4] = { .1, .01, .001, .0001 };
int four = 4;
int one = 1;
extern double sdot_();
int main() {
int i;
double r = sdot_(&four, x, &one, y, &one);
exit((float)r != (float).1234);
}" BLAS_F2C_DOUBLE_WORKS )
CHECK_C_SOURCE_RUNS("
#include <stdlib.h>
#include <stdio.h>
float x[4] = { 1, 2, 3, 4 };
float y[4] = { .1, .01, .001, .0001 };
int four = 4;
int one = 1;
extern float sdot_();
int main() {
int i;
double r = sdot_(&four, x, &one, y, &one);
exit((float)r != (float).1234);
}" BLAS_F2C_FLOAT_WORKS )
IF (BLAS_F2C_DOUBLE_WORKS AND NOT BLAS_F2C_FLOAT_WORKS)
MESSAGE(STATUS "This BLAS uses the F2C return conventions")
SET(BLAS_F2C TRUE)
ELSE (BLAS_F2C_DOUBLE_WORKS AND NOT BLAS_F2C_FLOAT_WORKS)
SET(BLAS_F2C FALSE)
ENDIF(BLAS_F2C_DOUBLE_WORKS AND NOT BLAS_F2C_FLOAT_WORKS)
CHECK_C_SOURCE_RUNS("
#include <stdlib.h>
#include <stdio.h>
float x[4] = { 1, 2, 3, 4 };
float y[4] = { .1, .01, .001, .0001 };
extern float cblas_sdot();
int main() {
int i;
double r = cblas_sdot(4, x, 1, y, 1);
exit((float)r != (float).1234);
}" BLAS_USE_CBLAS_DOT )
IF (BLAS_USE_CBLAS_DOT)
SET(BLAS_USE_CBLAS_DOT TRUE)
ELSE (BLAS_USE_CBLAS_DOT)
SET(BLAS_USE_CBLAS_DOT FALSE)
ENDIF(BLAS_USE_CBLAS_DOT)
SET(CMAKE_REQUIRED_LIBRARIES)

# Set values through env variables if cross compiling
IF (CMAKE_CROSSCOMPILING)
IF("$ENV{PYTORCH_BLAS_F2C}" STREQUAL "ON")
SET(BLAS_F2C TRUE)
ELSE()
SET(BLAS_F2C FALSE)
ENDIF()

IF("$ENV{PYTORCH_BLAS_USE_CBLAS_DOT}" STREQUAL "ON")
SET(BLAS_USE_CBLAS_DOT TRUE)
ELSE()
SET(BLAS_USE_CBLAS_DOT FALSE)
ENDIF()
ELSE ()
SET(CMAKE_REQUIRED_LIBRARIES ${BLAS_LIBRARIES})
CHECK_C_SOURCE_RUNS("
#include <stdlib.h>
#include <stdio.h>
float x[4] = { 1, 2, 3, 4 };
float y[4] = { .1, .01, .001, .0001 };
int four = 4;
int one = 1;
extern double sdot_();
int main() {
int i;
double r = sdot_(&four, x, &one, y, &one);
exit((float)r != (float).1234);
}" BLAS_F2C_DOUBLE_WORKS )
CHECK_C_SOURCE_RUNS("
#include <stdlib.h>
#include <stdio.h>
float x[4] = { 1, 2, 3, 4 };
float y[4] = { .1, .01, .001, .0001 };
int four = 4;
int one = 1;
extern float sdot_();
int main() {
int i;
double r = sdot_(&four, x, &one, y, &one);
exit((float)r != (float).1234);
}" BLAS_F2C_FLOAT_WORKS )
IF (BLAS_F2C_DOUBLE_WORKS AND NOT BLAS_F2C_FLOAT_WORKS)
MESSAGE(STATUS "This BLAS uses the F2C return conventions")
SET(BLAS_F2C TRUE)
ELSE (BLAS_F2C_DOUBLE_WORKS AND NOT BLAS_F2C_FLOAT_WORKS)
SET(BLAS_F2C FALSE)
ENDIF(BLAS_F2C_DOUBLE_WORKS AND NOT BLAS_F2C_FLOAT_WORKS)
CHECK_C_SOURCE_RUNS("
#include <stdlib.h>
#include <stdio.h>
float x[4] = { 1, 2, 3, 4 };
float y[4] = { .1, .01, .001, .0001 };
extern float cblas_sdot();
int main() {
int i;
double r = cblas_sdot(4, x, 1, y, 1);
exit((float)r != (float).1234);
}" BLAS_USE_CBLAS_DOT )
IF (BLAS_USE_CBLAS_DOT)
SET(BLAS_USE_CBLAS_DOT TRUE)
ELSE (BLAS_USE_CBLAS_DOT)
SET(BLAS_USE_CBLAS_DOT FALSE)
ENDIF(BLAS_USE_CBLAS_DOT)
SET(CMAKE_REQUIRED_LIBRARIES)
ENDIF(CMAKE_CROSSCOMPILING)
cmake_pop_check_state()
ENDIF(BLAS_LIBRARIES)

Expand Down
52 changes: 27 additions & 25 deletions cmake/public/cuda.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,33 @@ if(CUDA_FOUND)
" return 0;\n"
"}\n"
)
try_run(run_result compile_result ${PROJECT_RANDOM_BINARY_DIR} ${file}
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${CUDA_INCLUDE_DIRS}"
LINK_LIBRARIES ${CUDA_LIBRARIES}
RUN_OUTPUT_VARIABLE cuda_version_from_header
COMPILE_OUTPUT_VARIABLE output_var
)
if(NOT compile_result)
message(FATAL_ERROR "Caffe2: Couldn't determine version from header: " ${output_var})
endif()
message(STATUS "Caffe2: Header version is: " ${cuda_version_from_header})
if(NOT cuda_version_from_header STREQUAL ${CUDA_VERSION_STRING})
# Force CUDA to be processed for again next time
# TODO: I'm not sure if this counts as an implementation detail of
# FindCUDA
set(${cuda_version_from_findcuda} ${CUDA_VERSION_STRING})
unset(CUDA_TOOLKIT_ROOT_DIR_INTERNAL CACHE)
# Not strictly necessary, but for good luck.
unset(CUDA_VERSION CACHE)
# Error out
message(FATAL_ERROR "FindCUDA says CUDA version is ${cuda_version_from_findcuda} (usually determined by nvcc), "
"but the CUDA headers say the version is ${cuda_version_from_header}. This often occurs "
"when you set both CUDA_HOME and CUDA_NVCC_EXECUTABLE to "
"non-standard locations, without also setting PATH to point to the correct nvcc. "
"Perhaps, try re-running this command again with PATH=${CUDA_TOOLKIT_ROOT_DIR}/bin:$PATH. "
"See above log messages for more diagnostics, and see https://github.com/pytorch/pytorch/issues/8092 for more details.")
if(NOT CMAKE_CROSSCOMPILING)
try_run(run_result compile_result ${PROJECT_RANDOM_BINARY_DIR} ${file}
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${CUDA_INCLUDE_DIRS}"
LINK_LIBRARIES ${CUDA_LIBRARIES}
RUN_OUTPUT_VARIABLE cuda_version_from_header
COMPILE_OUTPUT_VARIABLE output_var
)
if(NOT compile_result)
message(FATAL_ERROR "Caffe2: Couldn't determine version from header: " ${output_var})
endif()
message(STATUS "Caffe2: Header version is: " ${cuda_version_from_header})
if(NOT cuda_version_from_header STREQUAL ${CUDA_VERSION_STRING})
# Force CUDA to be processed for again next time
# TODO: I'm not sure if this counts as an implementation detail of
# FindCUDA
set(${cuda_version_from_findcuda} ${CUDA_VERSION_STRING})
unset(CUDA_TOOLKIT_ROOT_DIR_INTERNAL CACHE)
# Not strictly necessary, but for good luck.
unset(CUDA_VERSION CACHE)
# Error out
message(FATAL_ERROR "FindCUDA says CUDA version is ${cuda_version_from_findcuda} (usually determined by nvcc), "
"but the CUDA headers say the version is ${cuda_version_from_header}. This often occurs "
"when you set both CUDA_HOME and CUDA_NVCC_EXECUTABLE to "
"non-standard locations, without also setting PATH to point to the correct nvcc. "
"Perhaps, try re-running this command again with PATH=${CUDA_TOOLKIT_ROOT_DIR}/bin:$PATH. "
"See above log messages for more diagnostics, and see https://github.com/pytorch/pytorch/issues/8092 for more details.")
endif()
endif()
endif()

Expand Down

0 comments on commit ee2dd35

Please sign in to comment.