Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ Thumbs.db
/config.yaml
/config.json

# cuFile deployment config (contains deployment-specific RDMA NIC IPs)
cufile.json

# cgo build output (if any)
*.a
*.o
Expand Down
11 changes: 10 additions & 1 deletion kv_connectors/llmd_fs_backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@ WHEEL_DIR = wheels
DIST_DIR = dist
VERSION := $(shell $(PYTHON) -c "import tomllib; print(tomllib.load(open('pyproject.toml','rb'))['project']['version'])")

.PHONY: wheel build test clean
.PHONY: wheel build test clean check-gds

# Install build dependencies
deps:
$(PYTHON) -m pip install --upgrade pip setuptools wheel build ninja numpy

# Check if GDS (cuFile) is available at runtime
check-gds:
@echo "Checking for GPUDirect Storage (cuFile) runtime availability..."
@if ldconfig -p 2>/dev/null | grep -q libcufile.so; then \
echo "✓ cuFile library found - GDS will be available at runtime"; \
else \
echo "✗ cuFile library not found - GDS will not be available at runtime (CPU buffer staging only)"; \
fi

# Build wheel into dist/ and copy .whl into wheels/
wheel: deps
$(PYTHON) -m pip wheel . -w $(DIST_DIR)
Expand Down
1 change: 1 addition & 0 deletions kv_connectors/llmd_fs_backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pip install -e .
- `block_size`: number of tokens stored per file (must be in granulaity of GPU block size).
- `threads_per_gpu`: number of I/O threads per GPU
- `max_staging_memory_gb`: total staging memory limit
- `gds_mode`: GPUDirect Storage mode (default: `disabled`). See [GPUDirect Storage (GDS)](./docs/gds.md) for options, requirements, and verification.

### Environment variables
- `STORAGE_LOG_LEVEL`: set the log level for both C++ and Python (`trace`, `debug`, `info`, `warn`, `error`). Default: `info`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright 2025 The llm-d Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <dlfcn.h>
#include <cstddef>
#include <cstdint>

// cuFile operation status codes
enum CUfileOpError { CU_FILE_SUCCESS = 0 };

// File handle types for cuFileHandleRegister
enum CUfileFileHandleType { CU_FILE_HANDLE_TYPE_OPAQUE_FD = 1 };

// Buffer registration flag for RDMA-capable memory
#define CU_FILE_RDMA_REGISTER 1

// Return type for most cuFile API calls
struct CUfileError_t {
CUfileOpError err;
};

// Forward declaration for filesystem operations table
struct CUfileFSOps_t;

// File descriptor passed to cuFileHandleRegister (layout must match cufile.h)
struct CUfileDescr_t {
CUfileFileHandleType type;
union {
int fd;
void* handle;
} handle;
const CUfileFSOps_t* fs_ops;
};

// Opaque handle returned by cuFileHandleRegister
typedef void* CUfileHandle_t;

// Driver properties returned by cuFileDriverGetProperties
struct CUfileDrvProps_t {
size_t max_device_cache_size;
size_t max_device_pinned_mem_size;
char _reserved[256];
};
Comment on lines +40 to +58
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🌐 Web query:

What is the struct layout for CUfileDescr_t and CUfileDrvProps_t in NVIDIA cuFile library version 1.x?

💡 Result:

The struct layouts for CUfileDescr_t and CUfileDrvProps_t in NVIDIA cuFile library version 1.x, as documented in official NVIDIA GPUDirect Storage cuFile API references (across CUDA 11.4-11.8 and current GDS v1.16 docs), are as follows: CUfileDescr_t: typedef struct CUfileDescr_t { CUfileFileHandleType type; /* type of file being registered / union { int fd; / Linux */ void handle; / Windows */ } handle; const CUfileFSOps_t fs_ops; / file system operation table / } CUfileDescr_t; CUfileDrvProps_t: typedef struct CUfileDrvProps { struct { unsigned int major_version; unsigned int minor_version; size_t poll_thresh_size; size_t max_direct_io_size; unsigned int dstatusflags; unsigned int dcontrolflags; } nvfs; CUfileFeatureFlags_t fflags; unsigned int max_device_cache_size; unsigned int per_buffer_cache_size; unsigned int max_pinned_memory_size; unsigned int max_batch_io_timeout_msecs; / present in some versions like 11.6 */ } CUfileDrvProps_t; Note: Minor variations exist across exact versions (e.g., max_batch_io_timeout_msecs appears in CUDA 11.6+ docs), but the core layout is consistent. The structure is self-describing per cuFileDriverGetProperties documentation. Layouts match across PDF docs, HTML API guides, and manpages for libcufile-dev 1.5+.

Citations:


Critical ABI mismatch in CUfileDrvProps_t struct definition—will cause memory corruption.

The CUfileDrvProps_t struct defined in this file does not match NVIDIA's official cuFile API definition. According to NVIDIA's cuFile documentation (CUDA 11.4–11.8, GDS v1.16), the actual struct contains:

  • A nested nvfs struct with version and configuration fields
  • CUfileFeatureFlags_t fflags
  • unsigned int field types (not size_t)
  • Fields named max_pinned_memory_size and per_buffer_cache_size (not max_device_pinned_mem_size)
  • Optional max_batch_io_timeout_msecs in newer versions

The code's simplified definition with _reserved[256] padding will misalign with the actual library structure, causing memory corruption when cuFileDriverGetProperties() writes to it. Fix struct definition to match NVIDIA's headers exactly, or use the official NVIDIA-provided header directly instead of recreating it. CUfileDescr_t is correctly defined.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@kv_connectors/llmd_fs_backend/csrc/storage/backends/fs_gds/cufile_loader.hpp`
around lines 40 - 58, The CUfileDrvProps_t definition here mismatches the
official NVIDIA cuFile ABI and can corrupt memory when
cuFileDriverGetProperties() populates it; replace the local struct with the
exact definition from NVIDIA's cuFile headers (or include the vendor header
directly) so fields like the nested nvfs struct, CUfileFeatureFlags_t fflags,
unsigned int-typed fields (e.g., max_pinned_memory_size, per_buffer_cache_size),
and any optional max_batch_io_timeout_msecs are present and correctly typed and
ordered; keep CUfileDescr_t as-is but remove or replace the _reserved[256]
placeholder and ensure CUfileDrvProps_t matches the vendor header exactly.


// CuFileApi is a runtime wrapper for the NVIDIA cuFile (GDS) library,
// so the same wheel works with or without GDS. Loads libcufile.so via dlopen.
// Function pointers are resolved via dlsym by symbol name (e.g. "cuFileRead").
// Singleton — library is loaded once and function pointers are reused.
class CuFileApi {
public:
static CuFileApi& instance() {
static CuFileApi loader;
return loader;
}

bool is_loaded() const { return m_handle != nullptr; }

// Function signature types — define what each cuFile function looks like
using FnDriverOpen = CUfileError_t (*)();
using FnDriverClose = CUfileError_t (*)();
using FnGetVersion = CUfileError_t (*)(int*);
using FnDriverGetProperties = CUfileError_t (*)(CUfileDrvProps_t*);
using FnBufRegister = CUfileError_t (*)(const void*, size_t, int);
using FnBufDeregister = CUfileError_t (*)(const void*);
using FnHandleRegister = CUfileError_t (*)(CUfileHandle_t*, CUfileDescr_t*);
using FnHandleDeregister = void (*)(CUfileHandle_t);
using FnRead = ssize_t (*)(CUfileHandle_t, void*, size_t, off_t, off_t);
using FnWrite =
ssize_t (*)(CUfileHandle_t, const void*, size_t, off_t, off_t);

// Resolved function pointers — null if library not loaded, filled by
// constructor via dlsym
FnDriverOpen cuFileDriverOpen = nullptr;
FnDriverClose cuFileDriverClose = nullptr;
FnGetVersion cuFileGetVersion = nullptr;
FnDriverGetProperties cuFileDriverGetProperties = nullptr;
FnBufRegister cuFileBufRegister = nullptr;
FnBufDeregister cuFileBufDeregister = nullptr;
FnHandleRegister cuFileHandleRegister = nullptr;
FnHandleDeregister cuFileHandleDeregister = nullptr;
FnRead cuFileRead = nullptr;
FnWrite cuFileWrite = nullptr;

private:
void* m_handle = nullptr;

// Attempts to load libcufile.so and resolve all function symbols.
// If the library or any symbol is missing, m_handle stays null.
CuFileApi() {
m_handle = dlopen("libcufile.so", RTLD_NOW);
if (!m_handle) {
// Try versioned name
m_handle = dlopen("libcufile.so.0", RTLD_NOW);
}
if (!m_handle) return;

cuFileDriverOpen =
reinterpret_cast<FnDriverOpen>(dlsym(m_handle, "cuFileDriverOpen"));
cuFileDriverClose =
reinterpret_cast<FnDriverClose>(dlsym(m_handle, "cuFileDriverClose"));
cuFileGetVersion =
reinterpret_cast<FnGetVersion>(dlsym(m_handle, "cuFileGetVersion"));
cuFileDriverGetProperties = reinterpret_cast<FnDriverGetProperties>(
dlsym(m_handle, "cuFileDriverGetProperties"));
cuFileBufRegister =
reinterpret_cast<FnBufRegister>(dlsym(m_handle, "cuFileBufRegister"));
cuFileBufDeregister = reinterpret_cast<FnBufDeregister>(
dlsym(m_handle, "cuFileBufDeregister"));
cuFileHandleRegister = reinterpret_cast<FnHandleRegister>(
dlsym(m_handle, "cuFileHandleRegister"));
cuFileHandleDeregister = reinterpret_cast<FnHandleDeregister>(
dlsym(m_handle, "cuFileHandleDeregister"));
cuFileRead = reinterpret_cast<FnRead>(dlsym(m_handle, "cuFileRead"));
cuFileWrite = reinterpret_cast<FnWrite>(dlsym(m_handle, "cuFileWrite"));

// Verify all symbols resolved
if (!cuFileDriverOpen || !cuFileDriverClose || !cuFileGetVersion ||
!cuFileBufRegister || !cuFileBufDeregister || !cuFileHandleRegister ||
!cuFileHandleDeregister || !cuFileRead || !cuFileWrite) {
dlclose(m_handle);
m_handle = nullptr;
}
}

// Unloads the library when the process exits
~CuFileApi() {
if (m_handle) {
dlclose(m_handle);
}
}
};
Loading
Loading