Skip to content

Commit ba616e2

Browse files
authored
Python Backend Windows Support (triton-inference-server#294)
* Base Python Backend Support for Windows
1 parent 0371eb8 commit ba616e2

16 files changed

+629
-261
lines changed

CMakeLists.txt

Lines changed: 92 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ option(TRITON_ENABLE_GPU "Enable GPU support in backend" ON)
4141
option(TRITON_ENABLE_STATS "Include statistics collections in backend" ON)
4242
option(TRITON_ENABLE_NVTX "Include nvtx markers collection in backend." OFF)
4343

44+
# FIXME: CI needs to enable the GPU flag. Python for window currently does not
45+
# support GPU tensors. For simplicity, we will override this option here.
46+
if(WIN32)
47+
set(TRITON_ENABLE_GPU OFF CACHE BOOL "GPU disabled" FORCE)
48+
endif()
49+
4450
set(TRITON_BACKEND_REPO_TAG "main" CACHE STRING "Tag for triton-inference-server/backend repo")
4551
set(TRITON_COMMON_REPO_TAG "main" CACHE STRING "Tag for triton-inference-server/common repo")
4652
set(TRITON_CORE_REPO_TAG "main" CACHE STRING "Tag for triton-inference-server/core repo")
@@ -96,6 +102,9 @@ FetchContent_Declare(
96102
GIT_TAG "v0.8"
97103
GIT_SHALLOW ON
98104
)
105+
# Option must be set off so WIN32 build does not break
106+
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
107+
set(BUILD_MOCK OFF)
99108
FetchContent_MakeAvailable(dlpack)
100109

101110
#
@@ -129,7 +138,10 @@ if(${TRITON_ENABLE_NVTX})
129138
endif() # TRITON_ENABLE_NVTX
130139

131140
find_package(ZLIB REQUIRED)
132-
find_package(Threads REQUIRED)
141+
142+
if(NOT WIN32)
143+
find_package(Threads REQUIRED)
144+
endif()
133145

134146
include_directories(${CMAKE_BINARY_DIR})
135147
configure_file(src/libtriton_python.ldscript libtriton_python.ldscript COPYONLY)
@@ -174,21 +186,21 @@ set(
174186
)
175187

176188
set(
177-
PYTHON_BACKEND_SRCS
178-
src/python_be.cc
179-
src/python_be.h
180-
src/pb_env.cc
181-
src/pb_env.h
182-
src/pb_metric_reporter.cc
183-
src/pb_metric_reporter.h
184-
src/memory_manager.cc
185-
src/memory_manager.h
186-
src/request_executor.cc
187-
src/request_executor.h
188-
src/stub_launcher.h
189-
src/stub_launcher.cc
190-
src/infer_payload.h
191-
src/infer_payload.cc
189+
PYTHON_BACKEND_SRCS
190+
src/python_be.cc
191+
src/python_be.h
192+
src/pb_env.cc
193+
src/pb_env.h
194+
src/pb_metric_reporter.cc
195+
src/pb_metric_reporter.h
196+
src/memory_manager.cc
197+
src/memory_manager.h
198+
src/request_executor.cc
199+
src/request_executor.h
200+
src/stub_launcher.h
201+
src/stub_launcher.cc
202+
src/infer_payload.h
203+
src/infer_payload.cc
192204
)
193205

194206
list(APPEND
@@ -239,48 +251,82 @@ target_compile_options(
239251
triton-python-backend PRIVATE
240252
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
241253
-Wall -Wextra -Wno-unused-parameter -Wno-type-limits -Werror>
254+
$<$<CXX_COMPILER_ID:MSVC>:/Wall /D_WIN32_WINNT=0x0A00 /EHsc /Zc:preprocessor>
242255
)
243256

244257
target_compile_features(triton-python-backend-stub PRIVATE cxx_std_${TRITON_MIN_CXX_STANDARD})
245258
target_compile_options(
246259
triton-python-backend-stub PRIVATE
247260
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
248-
-fvisibility=hidden -Wall -Wextra -Wno-unused-parameter -Wno-type-limits -Werror>
261+
-fvisibility=hidden -Wall -Wextra -Wno-unused-parameter -Wno-type-limits -Werror>
262+
$<$<CXX_COMPILER_ID:MSVC>:/Wall /D_WIN32_WINNT=0x0A00 /EHsc /Zc:preprocessor>
249263
)
250264
target_compile_definitions(triton-python-backend-stub PRIVATE TRITON_PB_STUB)
251265

252-
target_link_libraries(
253-
triton-python-backend
254-
PRIVATE
266+
# For WIN32 do not link Threads and DL_LIBS
267+
if(WIN32)
268+
target_link_libraries(
269+
triton-python-backend
270+
PRIVATE
271+
dlpack
272+
triton-backend-utils # from repo-backend
273+
-lrt # shared memory
274+
triton-core-serverstub # from repo-core
275+
ZLIB::ZLIB
276+
-larchive
277+
)
278+
279+
target_link_libraries(
280+
triton-python-backend-stub
281+
PRIVATE
282+
dlpack
283+
triton-backend-utils # from repo-backend
284+
pybind11::embed
285+
-lrt # shared memory
286+
-larchive # libarchive
287+
)
288+
else()
289+
target_link_libraries(
290+
triton-python-backend
291+
PRIVATE
292+
dlpack
293+
Threads::Threads
294+
triton-backend-utils # from repo-backend
295+
${CMAKE_DL_LIBS} # dlopen and dlclose
296+
-lrt # shared memory
297+
triton-core-serverstub # from repo-core
298+
ZLIB::ZLIB
299+
-larchive
300+
)
301+
302+
target_link_libraries(
303+
triton-python-backend-stub
304+
PRIVATE
255305
dlpack
256306
Threads::Threads
257-
triton-backend-utils # from repo-backend
258-
${CMAKE_DL_LIBS} # dlopen and dlclose
259-
-lrt # shared memory
260-
triton-core-serverstub # from repo-core
261-
ZLIB::ZLIB
262-
-larchive
263-
)
264-
265-
target_link_libraries(
266-
triton-python-backend-stub
267-
PRIVATE
268-
dlpack
269-
Threads::Threads
270-
triton-backend-utils # from repo-backend
271-
${CMAKE_DL_LIBS} # dlopen and dlclose
272-
pybind11::embed
273-
-lrt # shared memory
274-
-larchive # libarchive
275-
)
307+
triton-backend-utils # from repo-backend
308+
${CMAKE_DL_LIBS} # dlopen and dlclose
309+
pybind11::embed
310+
-lrt # shared memory
311+
-larchive # libarchive
312+
)
313+
endif()
276314

277-
set_target_properties(
278-
triton-python-backend PROPERTIES
279-
POSITION_INDEPENDENT_CODE ON
280-
OUTPUT_NAME triton_python
281-
LINK_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/libtriton_python.ldscript
282-
LINK_FLAGS "-Wl,--version-script libtriton_python.ldscript"
283-
)
315+
if(WIN32)
316+
set_target_properties(
317+
triton-python-backend PROPERTIES
318+
POSITION_INDEPENDENT_CODE ON
319+
OUTPUT_NAME triton_python
320+
)
321+
else()
322+
set_target_properties(
323+
triton-python-backend PROPERTIES
324+
POSITION_INDEPENDENT_CODE ON
325+
OUTPUT_NAME triton_python
326+
LINK_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/libtriton_python.ldscript
327+
LINK_FLAGS "-Wl,--version-script libtriton_python.ldscript"
328+
)
329+
endif()
284330

285331
add_subdirectory(./src/shm_monitor)
286332

src/infer_request.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class InferRequest {
8787
const uint64_t timeout = 0, const intptr_t response_factory_address = 0,
8888
const intptr_t request_address = 0,
8989
const PreferredMemory& preferred_memory =
90-
PreferredMemory(PreferredMemory::DEFAULT, 0),
90+
PreferredMemory(PreferredMemory::kDefault, 0),
9191
const InferenceTrace& trace = InferenceTrace());
9292

9393
const std::vector<std::shared_ptr<PbTensor>>& Inputs();

src/metric_family.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ TRITONSERVER_MetricKind
201201
MetricFamily::ToTritonServerMetricKind(const MetricKind& kind)
202202
{
203203
switch (kind) {
204-
case COUNTER:
204+
case kCounter:
205205
return TRITONSERVER_METRIC_KIND_COUNTER;
206-
case GAUGE:
206+
case kGauge:
207207
return TRITONSERVER_METRIC_KIND_GAUGE;
208208
default:
209209
throw PythonBackendException("Unknown metric kind");

src/pb_env.cc

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626

2727
#include "pb_env.h"
2828

29+
#ifndef _WIN32
2930
#include <archive.h>
3031
#include <archive_entry.h>
3132
#include <fts.h>
33+
#endif
3234
#include <sys/stat.h>
3335

3436
#include <cstdlib>
@@ -40,6 +42,29 @@
4042

4143
namespace triton { namespace backend { namespace python {
4244

45+
bool
46+
FileExists(std::string& path)
47+
{
48+
struct stat buffer;
49+
return stat(path.c_str(), &buffer) == 0;
50+
}
51+
52+
void
53+
LastModifiedTime(const std::string& path, time_t* last_modified_time)
54+
{
55+
struct stat result;
56+
if (stat(path.c_str(), &result) == 0) {
57+
*last_modified_time = result.st_mtime;
58+
} else {
59+
throw PythonBackendException(std::string(
60+
"LastModifiedTime() failed as file \'" + path +
61+
std::string("\' does not exists.")));
62+
}
63+
}
64+
65+
// FIXME: [DLIS-5969]: Develop platforom-agnostic functions
66+
// to support custom python environments.
67+
#ifndef _WIN32
4368
void
4469
CopySingleArchiveEntry(archive* input_archive, archive* output_archive)
4570
{
@@ -73,7 +98,6 @@ CopySingleArchiveEntry(archive* input_archive, archive* output_archive)
7398
}
7499
}
75100

76-
77101
void
78102
ExtractTarFile(std::string& archive_path, std::string& dst_path)
79103
{
@@ -153,27 +177,6 @@ ExtractTarFile(std::string& archive_path, std::string& dst_path)
153177
}
154178
}
155179

156-
bool
157-
FileExists(std::string& path)
158-
{
159-
struct stat buffer;
160-
return stat(path.c_str(), &buffer) == 0;
161-
}
162-
163-
void
164-
LastModifiedTime(const std::string& path, time_t* last_modified_time)
165-
{
166-
struct stat result;
167-
if (stat(path.c_str(), &result) == 0) {
168-
*last_modified_time = result.st_mtime;
169-
} else {
170-
throw PythonBackendException(std::string(
171-
"LastModifiedTime() failed as file \'" + path +
172-
std::string("\' does not exists.")));
173-
}
174-
}
175-
176-
177180
void
178181
RecursiveDirectoryDelete(const char* dir)
179182
{
@@ -326,5 +329,6 @@ EnvironmentManager::~EnvironmentManager()
326329
{
327330
RecursiveDirectoryDelete(base_path_);
328331
}
332+
#endif
329333

330334
}}} // namespace triton::backend::python

src/pb_env.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
#include <mutex>
3131
#include <string>
3232

33+
#ifdef WIN32
34+
#include <windows.h>
35+
#undef PATH_MAX
36+
#define PATH_MAX MAX_PATH
37+
#endif
3338
namespace triton { namespace backend { namespace python {
3439

3540
void ExtractTarFile(std::string& archive_path, std::string& dst_path);
@@ -39,6 +44,7 @@ bool FileExists(std::string& path);
3944
//
4045
// A class that manages Python environments
4146
//
47+
#ifndef _WIN32
4248
class EnvironmentManager {
4349
std::map<std::string, std::pair<std::string, time_t>> env_map_;
4450
char base_path_[PATH_MAX + 1];
@@ -52,5 +58,6 @@ class EnvironmentManager {
5258
std::string ExtractIfNotExtracted(std::string env_path);
5359
~EnvironmentManager();
5460
};
61+
#endif
5562

5663
}}} // namespace triton::backend::python

src/pb_preferred_memory.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ namespace triton { namespace backend { namespace python {
3030

3131
class PreferredMemory {
3232
public:
33-
enum MemoryType { GPU, CPU, DEFAULT };
33+
enum MemoryType { kGPU, kCPU, kDefault };
3434

3535
PreferredMemory()
36-
: preferred_memory_type_(MemoryType::DEFAULT), preferred_device_id_(0)
36+
: preferred_memory_type_(MemoryType::kDefault), preferred_device_id_(0)
3737
{
3838
}
3939

0 commit comments

Comments
 (0)