Skip to content

Commit 0afe591

Browse files
committed
Working on linking all of pytorch as a static lib.
1 parent 42fa424 commit 0afe591

8 files changed

Lines changed: 184 additions & 43 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ test/correspondence/gh.out
3636
!modules/*
3737
modules/build
3838
!bridge/*
39-
bridge/build
39+
bridge/build
40+
pytorch/build
41+
# pytorch/

bridge/CMakeLists.txt

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,12 @@ endif()
1111

1212
set(CMAKE_C_COMPILER "clang")
1313
set(CMAKE_CXX_COMPILER "clang++")
14-
set(CMAKE_CXX_STANDARD 17)
14+
set(CMAKE_CXX_STANDARD 11)
1515

1616

1717
set(PROJECT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
1818
set(PROJECT_BINARY_DIR "${CMAKE_BINARY_DIR}")
1919

20-
find_package(chpl REQUIRED HINTS ${PROJECT_ROOT_DIR}/cmake/chapel)
21-
list(APPEND CMAKE_MODULE_PATH "${PROJECT_ROOT_DIR}/cmake")
22-
list(APPEND CMAKE_MODULE_PATH "${PROJECT_ROOT_DIR}/cmake/chapel")
23-
24-
project(MyProject LANGUAGES CXX C CHPL)
25-
2620

2721
# For ExternalProject_Add
2822
include(FetchContent)
@@ -34,6 +28,10 @@ include(ExternalProject)
3428

3529
# Where to place PyTorch after installation
3630
set(PYTORCH_INSTALL_DIR "${PROJECT_BINARY_DIR}/pytorch-install")
31+
set(PYTORCH_BUILD_DIR "${PROJECT_BINARY_DIR}/pytorch-prefix/src/pytorch-build")
32+
33+
set(PYTORCH_INSTALL_PREFIX "${PROJECT_BINARY_DIR}/pytorch-install-prefix")
34+
file(MAKE_DIRECTORY "${PYTORCH_INSTALL_PREFIX}")
3735

3836
# ExternalProject_Add can fetch from Git, a local path, or a release tarball.
3937
# Here, for simplicity, we'll fetch from Git. In practice, you might want
@@ -64,17 +62,26 @@ ExternalProject_Add(
6462
-DUSE_CUDA=OFF # Disable CUDA
6563
-DUSE_CUDNN=OFF # Disable cuDNN
6664
-DUSE_MKLDNN=OFF # Disable MKLDNN for simplicity
65+
# -DBUILD_BINARY=ON
66+
# -DUSE_DISTRIBUTED=ON
67+
# -DBUILD_STATIC_RUNTIME_BENCHMARK=ON
68+
# -DBUILD_LITE_INTERPRETER=ON
69+
# -DUSE_STATIC_MKL=ON
70+
# -DSTATIC_DISPATCH_BACKEND=ON
71+
# -DCAFFE2_USE_MSVC_STATIC_RUNTIME=ON
72+
-DUSE_DISTRIBUTED=ON
6773
-DCMAKE_BUILD_TYPE=Release
68-
-DCMAKE_INSTALL_PREFIX=${PYTORCH_INSTALL_DIR}
74+
-DCMAKE_INSTALL_PREFIX=${PYTORCH_INSTALL_PREFIX}
75+
# -DCMAKE_POLICY_VERSION_MINIMUM=3.5
6976

7077
INSTALL_DIR ${PYTORCH_INSTALL_DIR} # Where to install
7178

72-
LOG_DOWNLOAD ON
73-
LOG_UPDATE ON
74-
LOG_PATCH ON
75-
LOG_CONFIGURE ON
79+
# LOG_DOWNLOAD ON
80+
# LOG_UPDATE ON
81+
# LOG_PATCH ON
82+
# LOG_CONFIGURE ON
7683
# LOG_BUILD ON
77-
LOG_INSTALL ON
84+
# LOG_INSTALL ON
7885

7986
)
8087

@@ -83,6 +90,23 @@ file(GLOB_RECURSE PYTORCH_INCLUDES "${PYTORCH_INSTALL_DIR}/include" "*.h")
8390
file(GLOB_RECURSE PYTORCH_LIBS "${PYTORCH_INSTALL_DIR}/lib" "*.a")
8491

8592

93+
set(PYTORCH_LIBS_LINKER_ARGS "") # Will hold the list of "-l..." flags.
94+
foreach(lib_path IN LISTS PYTORCH_LIBS)
95+
# Get just the filename without the directory or extension
96+
get_filename_component(lib_name "${lib_path}" NAME_WE)
97+
# If it starts with "lib", strip that off
98+
string(REGEX REPLACE "^lib" "" lib_name "${lib_name}")
99+
# Now prepend "-l" to the actual library name
100+
list(APPEND MY_LIBS "-l${lib_name}")
101+
endforeach()
102+
103+
104+
find_package(chpl REQUIRED HINTS ${PROJECT_ROOT_DIR}/cmake/chapel)
105+
list(APPEND CMAKE_MODULE_PATH "${PROJECT_ROOT_DIR}/cmake")
106+
list(APPEND CMAKE_MODULE_PATH "${PROJECT_ROOT_DIR}/cmake/chapel")
107+
108+
project(MyProject LANGUAGES CXX C CHPL)
109+
86110
# ------------------------------------------------------------------------------
87111
# 2) Create an INTERFACE library to wrap the installed static libs
88112
# ------------------------------------------------------------------------------
@@ -99,6 +123,7 @@ add_dependencies(torch_interface pytorch)
99123
target_include_directories(torch_interface INTERFACE
100124
"${PYTORCH_INSTALL_DIR}/include"
101125
"${PYTORCH_INSTALL_DIR}/include/torch/csrc/api/include"
126+
# "${PYTORCH_BUILD_DIR}/aten/src/include"
102127
)
103128

104129
# Link the relevant static libraries. For a minimal CPU-only build,
@@ -126,17 +151,22 @@ add_executable(CHPLTest lib/CHPLTest.chpl)
126151
add_library(torchbridge STATIC "${PROJECT_ROOT_DIR}/lib/bridge.cpp" "${PROJECT_ROOT_DIR}/include/bridge.h")
127152
add_dependencies(torchbridge torch_interface)
128153

129-
target_include_directories(torchbridge PUBLIC
154+
target_include_directories(torchbridge PRIVATE
130155
"${PYTORCH_INSTALL_DIR}/include"
131156
"${PYTORCH_INSTALL_DIR}/include/torch/csrc/api/include"
132157
"${PROJECT_ROOT_DIR}/include"
158+
# "${PYTORCH_BUILD_DIR}/aten/src/include"
133159
)
134160
# target_link_directories(torchbridge PUBLIC
135161
# "${PYTORCH_INSTALL_DIR}/lib"
136162
# )
137163
target_link_libraries(torchbridge
138-
torch_interface
139-
${PYTORCH_LIBS}
164+
PRIVATE
165+
torch_interface
166+
${PYTORCH_LIBS}
167+
pthread
168+
dl
169+
rt
140170
)
141171

142172

@@ -145,23 +175,29 @@ target_link_libraries(torchbridge
145175

146176
add_executable(Bridge lib/Bridge.chpl include/bridge.h)
147177
# add_dependencies(TorchBridge torchbridge)
178+
add_dependencies(Bridge torchbridge)
148179

149180
target_link_options(Bridge
150181
PRIVATE
151182
"${PROJECT_ROOT_DIR}/include/bridge.h"
152183
"-L${PROJECT_BINARY_DIR}"
184+
"-ltorchbridge"
153185
-L.
154186
"-ltorchbridge"
155-
"-I${PROJECT_BINARY_DIR}"
156-
"-L${PYTORCH_INSTALL_DIR}/lib"
157-
"-I${PYTORCH_INSTALL_DIR}/include"
158-
"-I${PYTORCH_INSTALL_DIR}/include/torch/csrc/api/include"
159-
"-ltorch"
160-
"-ltorch_cpu"
161-
"-lcpuinfo"
162-
"-lc10"
163-
"-lpthread"
164-
"-ldl"
187+
# "-I${PROJECT_BINARY_DIR}"
188+
# "-L${PYTORCH_INSTALL_DIR}/lib"
189+
# "-I${PYTORCH_INSTALL_DIR}/include"
190+
# "-I${PYTORCH_INSTALL_DIR}/include/torch/csrc/api/include"
191+
# "-ltorch"
192+
# "-ltorch_cpu"
193+
# "-lcpuinfo"
194+
# "-lc10"
195+
# "-lsleef"
196+
# "-lclog"
197+
# "-lprotoc"
198+
# ${PYTORCH_LIBS_LINKER_ARGS}
199+
# "-lpthread"
200+
# "-ldl"
165201
)
166202

167203

bridge/include/bridge.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
#include <stdio.h>
55

66
#ifdef __cplusplus
7-
#include <torch/torch.h>
8-
#include <iostream>
9-
#include <vector>
107
extern "C" {
118
#endif
129

bridge/lib/Bridge.chpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ extern proc wrHello(): void;
66
extern proc wrHelloTorch(): void;
77
extern proc sumArray(arr: [] real(32), sizes: [] int(32), dim: int(32)): real(32);
88

9-
wrHello();
9+
10+
baz();
11+
1012
wrHello();
1113

1214

bridge/lib/bridge.cpp

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
#include <bridge.h>
22

3+
#include <torch/torch.h>
4+
// #include <torch/script.h>
5+
// #include <Aten/ATen.h>
6+
#include <iostream>
7+
#include <vector>
8+
9+
void secret() {
10+
std::cout << "Secret function called!" << std::endl;
11+
torch::Tensor tensor = torch::eye(3);
12+
// std::cout << "Tensor: " << tensor << std::endl;
13+
14+
// std::cout << "Secret function called! Tensor: " << tensor << std::endl;
15+
}
316

417
extern "C" int baz(void) {
5-
auto x = torch::randn({5, 3});
6-
return x.size(0);
18+
printf("Hello from baz!\n");
19+
secret();
20+
// auto x = torch::randn({5, 3});
21+
// return x.size(0);
722
}
823

924

@@ -14,27 +29,27 @@ extern "C" void wrHello(void) {
1429

1530
extern "C" void wrHelloTorch(void) {
1631
printf("Hello from wrHelloTorch!\n");
17-
auto t = torch::ones({2, 3});
18-
std::cout << t << std::endl;
32+
// auto t = torch::ones({2, 3});
33+
// std::cout << t << std::endl;
1934
}
2035

2136
extern "C" float sumArray(float* arr, int* sizes, int dim) {
2237
// Convert sizes to std::vector<int64_t>
2338

2439
printf("sumArray called with arr: %p, sizes: %p, dim: %d\n", arr, sizes, dim);
2540

26-
std::vector<int64_t> sizes_vec(sizes, sizes + dim);
27-
std::cout << sizes_vec << std::endl;
41+
// std::vector<int64_t> sizes_vec(sizes, sizes + dim);
42+
// std::cout << sizes_vec << std::endl;
2843

29-
auto shape = at::IntArrayRef(sizes_vec);
30-
std::cout << shape << std::endl;
44+
// auto shape = at::IntArrayRef(sizes_vec);
45+
// std::cout << shape << std::endl;
3146

32-
auto t = torch::from_blob(arr, shape, torch::kFloat);
33-
std::cout << t << std::endl;
47+
// auto t = torch::from_blob(arr, shape, torch::kFloat);
48+
// std::cout << t << std::endl;
3449

35-
return t.sum().item<float>();
50+
// return t.sum().item<float>();
3651

37-
// return 0.0f;
52+
return 0.0f;
3853

3954
// float sum = 0.0f;
4055
// for (int i = 0; i < size; ++i) {

pytorch

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 58ede0cca3e73a05659bd8de8f131f2e83601b5c

requirements.txt

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
annotated-types==0.7.0
2+
anyio==4.8.0
3+
appnope==0.1.4
4+
asttokens==3.0.0
5+
astunparse==1.6.3
6+
attrs==25.3.0
7+
certifi==2024.12.14
8+
charset-normalizer==3.4.1
9+
cmake==4.0.0
10+
comm==0.2.2
11+
contourpy==1.3.1
12+
cycler==0.12.1
13+
debugpy==1.8.12
14+
decorator==5.1.1
15+
executing==2.1.0
16+
expecttest==0.3.0
17+
filelock==3.16.1
18+
fonttools==4.56.0
19+
fsspec==2024.12.0
20+
h11==0.14.0
21+
httpcore==1.0.7
22+
httpx==0.28.1
23+
hypothesis==6.130.10
24+
idna==3.10
25+
ipykernel==6.29.5
26+
ipython==8.31.0
27+
jedi==0.19.2
28+
Jinja2==3.1.5
29+
jupyter_client==8.6.3
30+
jupyter_core==5.7.2
31+
kagglehub==0.3.10
32+
kiwisolver==1.4.8
33+
lintrunner==0.12.7
34+
MarkupSafe==3.0.2
35+
matplotlib==3.10.1
36+
matplotlib-inline==0.1.7
37+
mpmath==1.3.0
38+
nest-asyncio==1.6.0
39+
networkx==3.4.2
40+
ninja==1.11.1.4
41+
numpy==2.2.1
42+
ollama==0.4.7
43+
opencv-python==4.11.0.86
44+
optree==0.15.0
45+
packaging==24.2
46+
parso==0.8.4
47+
pexpect==4.9.0
48+
pillow==11.0.0
49+
platformdirs==4.3.6
50+
prompt_toolkit==3.0.48
51+
psutil==6.1.1
52+
ptyprocess==0.7.0
53+
pure_eval==0.2.3
54+
pydantic==2.10.6
55+
pydantic_core==2.27.2
56+
Pygments==2.19.1
57+
pyparsing==3.2.1
58+
python-dateutil==2.9.0.post0
59+
PyYAML==6.0.2
60+
pyzmq==26.2.0
61+
requests==2.32.3
62+
setuptools==75.8.0
63+
six==1.17.0
64+
sniffio==1.3.1
65+
sortedcontainers==2.4.0
66+
stack-data==0.6.3
67+
sympy==1.13.3
68+
tinygrad==0.10.2
69+
torch==2.5.1
70+
torchaudio==2.5.1
71+
torchvision==0.20.1
72+
tornado==6.4.2
73+
tqdm==4.67.1
74+
traitlets==5.14.3
75+
types-dataclasses==0.6.6
76+
typing_extensions==4.12.2
77+
urllib3==2.3.0
78+
wcwidth==0.2.13
79+
wheel==0.45.1

to_run.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
git clone --recursive https://github.com/pytorch/pytorch
2+
cd pytorch
3+
pip install cmake ninja
4+
pip install -r requirements.txt
5+
pip install pkg-config libuv
6+
MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ python setup.py build --cmake-only
7+
8+
9+
python3 setup.py develop

0 commit comments

Comments
 (0)