Skip to content

Commit 859f60c

Browse files
committed
Update PyTorch API example.
1 parent 84cc5b9 commit 859f60c

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

PyTorch/CMakeLists.txt

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
1-
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
2-
project("dcgan")
1+
cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
32

4-
set(Torch_DIR "C:/libtorch/")
5-
set(CMAKE_PREFIX_PATH "C:/libtorch/share/cmake/")
3+
project("dcgan")
64

5+
# Ensure the path to libtorch is correct. Adjust it according to your installation.
6+
set(Torch_DIR "D:/include/libtorch/share/cmake/Torch")
7+
set(Torch_INCLUDE_DIRS "D:/include/libtorch/include")
78
find_package(Torch REQUIRED)
89

9-
add_executable(dcgan "dcgan.cpp")
10+
# Include the Torch headers
11+
include_directories(${Torch_INCLUDE_DIRS})
1012

11-
target_link_libraries(dcgan
12-
"${TORCH_LIBRARIES}"
13-
"C:/libtorch/lib/c10.dll"
14-
"C:/libtorch/lib/torch_cpu.dll"
15-
)
13+
# Set any necessary compile flags
14+
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
15+
# Compiler is Microsoft Visual C++
16+
# Set MSVC-specific flags
17+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /Zc:__cplusplus /EHsc /std:c++latest /experimental:module")
18+
endif()
1619

20+
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
21+
# Compiler is g++
22+
# Set g++-specific flags
23+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
24+
endif()
25+
26+
add_executable(dcgan "dcgan.cpp")
27+
target_link_libraries(dcgan "${TORCH_LIBRARIES}")
1728
set_property(TARGET dcgan PROPERTY CXX_STANDARD 20)
29+
30+
# The following code block is suggested to be used on Windows.
31+
# According to https://github.com/pytorch/pytorch/issues/25457,
32+
# the DLLs need to be copied to avoid memory errors.
33+
if(MSVC)
34+
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
35+
add_custom_command(TARGET dcgan
36+
POST_BUILD
37+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
38+
${TORCH_DLLS}
39+
$<TARGET_FILE_DIR:dcgan>)
40+
endif(MSVC)

PyTorch/dcgan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct Net : torch::nn::Module {
1515
torch::Tensor another_bias;
1616
};
1717

18-
auto main(void) -> int {
18+
int main(void) {
1919
torch::Tensor tensor = torch::eye(3);
2020
std::cout << tensor << "\n";
2121

0 commit comments

Comments
 (0)