-
Notifications
You must be signed in to change notification settings - Fork 56
fix(flux): check the return value of ensure_buf's cudaMalloc #1283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chaofengw-nv
merged 1 commit into
NVIDIA:main
from
Moviw:fix/flux-matmul-scratch-alloc-check
Sep 15, 2026
+221
−32
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <cuda_runtime_api.h> | ||
| #include <stdexcept> | ||
|
|
||
| namespace trtmc { | ||
| namespace flux { | ||
|
|
||
| // Owns one device allocation. Growing it frees the previous allocation before | ||
| // attempting the new one and leaves the pointer null on failure, so a failed | ||
| // grow can never leave a stale or dangling pointer for the caller to reuse. | ||
| class DeviceBuffer { | ||
| public: | ||
| DeviceBuffer() = default; | ||
|
|
||
| ~DeviceBuffer() { cudaFree(ptr_); } | ||
|
|
||
| DeviceBuffer(const DeviceBuffer&) = delete; | ||
| DeviceBuffer& operator=(const DeviceBuffer&) = delete; | ||
|
|
||
| DeviceBuffer(DeviceBuffer&& other) noexcept : ptr_(other.ptr_) { other.ptr_ = nullptr; } | ||
|
|
||
| DeviceBuffer& operator=(DeviceBuffer&& other) noexcept { | ||
| if (this != &other) { | ||
| cudaFree(ptr_); | ||
| ptr_ = other.ptr_; | ||
| other.ptr_ = nullptr; | ||
| } | ||
| return *this; | ||
| } | ||
|
|
||
| cudaError_t allocate(std::size_t bytes) { | ||
| cudaFree(ptr_); | ||
| ptr_ = nullptr; | ||
| return cudaMalloc(&ptr_, bytes); | ||
| } | ||
|
|
||
| void* get() const { return ptr_; } | ||
|
|
||
| private: | ||
| void* ptr_{nullptr}; | ||
| }; | ||
|
|
||
| // A device buffer reused and grown across calls (unlike the per-request | ||
| // buffers the other families own): `bytes` tracks the capacity actually | ||
| // allocated, not the size of the most recent request. | ||
| struct GrowableBuffer { | ||
| DeviceBuffer buf; | ||
| std::size_t bytes = 0; | ||
| }; | ||
|
|
||
| // Grows `gb` to at least `need` bytes. A no-op when it is already large | ||
| // enough. On allocation failure the previous (too-small) buffer is already | ||
| // gone -- `bytes` is left unchanged so the next call retries the grow rather | ||
| // than treating the missing buffer as already sized. | ||
| inline void ensure_buf(GrowableBuffer& gb, std::size_t need) { | ||
| if (gb.bytes >= need) | ||
| return; | ||
| if (gb.buf.allocate(need) != cudaSuccess) | ||
| throw std::runtime_error("flux_gpu_matmul: unable to allocate device buffer"); | ||
| gb.bytes = need; | ||
| } | ||
|
|
||
| } // namespace flux | ||
| } // namespace trtmc | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
families/flux/tests/cpp/test_flux_device_buffer_alloc.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| // Exercises flux::ensure_buf's grow-and-retry contract against CPU CUDA | ||
| // stubs, so an allocation failure can be injected without a GPU. The matmul | ||
| // scratch buffers are process-lifetime globals reused across calls, unlike | ||
| // the per-request buffers the other families own, so what matters here is | ||
| // that a failed grow releases the stale buffer and leaves the tracked size | ||
| // unchanged, rather than the constructor-unwind behavior those test. | ||
|
|
||
| #include "families/flux/runtime/device_buffer.h" | ||
|
|
||
| #include <cstdint> | ||
| #include <cstdio> | ||
| #include <cuda_runtime.h> | ||
| #include <set> | ||
| #include <stdexcept> | ||
|
|
||
| namespace { | ||
|
|
||
| int g_fail_on_allocation = 0; | ||
| int g_allocation_count = 0; | ||
| std::set<void*> g_outstanding; | ||
| std::uintptr_t g_next_address = 0x1000; | ||
| int g_failures = 0; | ||
|
|
||
| void check(bool condition, const char* what) { | ||
| if (!condition) { | ||
| std::fprintf(stderr, "FAIL: %s\n", what); | ||
| ++g_failures; | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| extern "C" { | ||
|
|
||
| cudaError_t cudaMalloc(void** devPtr, size_t size) { | ||
| (void)size; | ||
| ++g_allocation_count; | ||
| if (g_fail_on_allocation != 0 && g_allocation_count == g_fail_on_allocation) { | ||
| *devPtr = nullptr; | ||
| return cudaErrorMemoryAllocation; | ||
| } | ||
| void* address = reinterpret_cast<void*>(g_next_address); | ||
| g_next_address += 0x1000; | ||
| g_outstanding.insert(address); | ||
| *devPtr = address; | ||
| return cudaSuccess; | ||
| } | ||
|
|
||
| cudaError_t cudaFree(void* devPtr) { | ||
| if (devPtr != nullptr) { | ||
| g_outstanding.erase(devPtr); | ||
| } | ||
| return cudaSuccess; | ||
| } | ||
|
|
||
| } // extern "C" | ||
|
|
||
| int main() { | ||
| using trtmc::flux::ensure_buf; | ||
| using trtmc::flux::GrowableBuffer; | ||
|
|
||
| // A no-op grow (need <= bytes) must not touch the allocator at all. | ||
| { | ||
| GrowableBuffer gb; | ||
| g_allocation_count = 0; | ||
| ensure_buf(gb, 256); | ||
| check(g_allocation_count == 1, "the first grow from empty should allocate once"); | ||
| const int32_t first_count = g_allocation_count; | ||
| ensure_buf(gb, 128); | ||
| check(g_allocation_count == first_count, | ||
| "shrinking the request below the current capacity should not reallocate"); | ||
| } | ||
| check(g_outstanding.empty(), "leaving scope should release the buffer"); | ||
|
|
||
| // A failed grow must release the stale buffer, leave the pointer null, | ||
| // and leave `bytes` unchanged so the next call retries rather than | ||
| // treating the missing buffer as already sized. | ||
| { | ||
| GrowableBuffer gb; | ||
| ensure_buf(gb, 256); | ||
| check(gb.bytes == 256, "a successful grow should record the new size"); | ||
|
|
||
| g_fail_on_allocation = g_allocation_count + 1; | ||
| bool threw = false; | ||
| try { | ||
| ensure_buf(gb, 1024); | ||
| } catch (const std::runtime_error&) { | ||
| threw = true; | ||
| } | ||
| check(threw, "a failed grow should throw"); | ||
| check(gb.buf.get() == nullptr, "a failed grow must leave the pointer null, not stale"); | ||
| check(gb.bytes == 256, "a failed grow must not update the tracked size"); | ||
| check(g_outstanding.empty(), | ||
| "a failed grow must release the old buffer instead of leaking it"); | ||
|
|
||
| // Retrying with the allocator working again must succeed instead of | ||
| // treating `bytes` as already covering the request. | ||
| g_fail_on_allocation = 0; | ||
| ensure_buf(gb, 1024); | ||
| check(gb.bytes == 1024, "retrying after a transient failure should grow normally"); | ||
| } | ||
| check(g_outstanding.empty(), "leaving scope should release the buffer"); | ||
|
|
||
| if (g_failures != 0) { | ||
| std::fprintf(stderr, "%d check(s) failed\n", g_failures); | ||
| return 1; | ||
| } | ||
| std::printf("flux device buffer allocation-failure checks passed\n"); | ||
| return 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Require a live allocation for the no-op path.
DeviceBuffer::allocatefrees the existing allocation and leavesbufnull when growth fails, whilebytesremains unchanged. A later smaller request can therefore passgb.bytes >= needand return without allocating.gpu_matmul.cppthen passes the null pointer to CUDA operations. Checkgb.buf.get()in this condition and add a regression test for this sequence.📝 Committable suggestion
🤖 Prompt for AI Agents