Skip to content

Commit ef013c0

Browse files
haijiegqelk123
andcommitted
Add support for cutile jax ffi
Co-authored-by: yinuol <yinuol@nvidia.com> Signed-off-by: Jay Gu <jagu@nvidia.com>
1 parent 2cd6dc4 commit ef013c0

24 files changed

Lines changed: 1709 additions & 13 deletions

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ else()
9090
message(STATUS "Fetching dlpack")
9191
endif()
9292

93+
include(cmake/FetchXLAHeaders.cmake)
94+
fetch_xla_headers()
95+
9396
add_subdirectory(cext)
9497

9598

cext/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ endif()
3636

3737
set(cext_include_dirs
3838
${dlpack_INCLUDE_DIR}
39+
${xla_INCLUDE_DIR}
3940
${Python_INCLUDE_DIRS}
40-
${CUDAToolkit_INCLUDE_DIRS})
41+
${CUDAToolkit_INCLUDE_DIRS}
42+
)
4143

4244

4345
# Build a static library first, so that we could reuse it for several build targets
@@ -49,6 +51,8 @@ add_library(_cext_static STATIC
4951
py.cpp
5052
stream_buffer.cpp
5153
tile_kernel.cpp
54+
xla_ffi.cpp
55+
xla_ffi_py.cpp
5256
)
5357

5458
target_compile_options(_cext_static PUBLIC ${cext_compile_flags} ${nostdlib_flags})

cext/cuda_loader.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,9 @@ static Status cuda_loader_init(DriverApi& driver_api) {
7777

7878
static constexpr int MIN_DRIVER_VERSION = 13000;
7979

80-
#ifdef Py_GIL_DISABLED
81-
static PyMutex g_driver_api_mutex = {0};
82-
#endif
83-
8480
Result<const DriverApi*> get_driver_api() {
8581
static bool initialized;
8682
static DriverApi instance;
87-
#ifdef Py_GIL_DISABLED
88-
PyCriticalSectionGuard guard(&g_driver_api_mutex);
89-
#endif
9083
if (!initialized) {
9184
if (!cuda_loader_init(instance))
9285
return ErrorRaised;

cext/cuda_loader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
X(cuEventDestroy, 2000) \
3434
X(cuEventQuery, 2000) \
3535
X(cuEventRecord, 2000) \
36+
X(cuKernelGetFunction, 12000) \
3637
X(cuMemAlloc, 3020) \
3738
X(cuMemAllocHost, 3020) \
3839
X(cuMemFree, 3020) \

cext/hash.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,3 @@ struct Hash<T, typename std::enable_if_t<std::is_enum_v<T>> > {
8989
return Hash<uint64_t>::hash(static_cast<uint64_t>(val), h);
9090
}
9191
};
92-
93-

cext/module.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "tile_kernel.h"
88
#include "cuda_helper.h"
9+
#include "xla_ffi_py.h"
910

1011
#ifdef _WIN32
1112
extern "C" int _fltused = 0;
@@ -47,6 +48,10 @@ PyMODINIT_FUNC PyInit__cext() {
4748
if (!cuda_helper_init(m.get()))
4849
return nullptr;
4950

51+
if (!xla_ffi_init(m.get()))
52+
return nullptr;
53+
54+
5055
return m.release();
5156
}
5257

cext/py.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,22 @@ static inline PyPtr try_import(const char* modname, SavedException* exc = nullpt
277277
return ret;
278278
}
279279

280+
class GILGuard {
281+
public:
282+
GILGuard(const GILGuard&) = delete;
283+
void operator=(const GILGuard&) = delete;
284+
285+
GILGuard() {
286+
gstate = PyGILState_Ensure();
287+
}
288+
289+
~GILGuard() {
290+
PyGILState_Release(gstate);
291+
}
292+
private:
293+
PyGILState_STATE gstate;
294+
};
295+
280296
#ifdef Py_GIL_DISABLED
281297
class PyCriticalSectionGuard {
282298
public:

0 commit comments

Comments
 (0)