Skip to content

Commit b20f16a

Browse files
authored
Cover caller-stream TLS semantics (pytorch#21291) (pytorch#21291)
Summary: Add focused coverage for the caller-stream contract relied on by external CUDA backends. Verify that an explicitly selected null/default stream remains distinct from no guard, and that selections are isolated per execution thread. Keep the fbcode and xplat mirrors identical. Differential Revision: D113382078
1 parent 8200b9e commit b20f16a

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

backends/aoti/slim/cuda/test/test_cuda_stream_guard.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <executorch/runtime/platform/platform.h>
1313
#include <gtest/gtest.h>
1414

15+
#include <thread>
1516
#include <type_traits>
1617

1718
using namespace executorch::backends::cuda;
@@ -296,6 +297,30 @@ TEST(CallerStreamGuardTest, GuardSelectsThenRestores) {
296297
EXPECT_FALSE(getCallerStream().has_value());
297298
}
298299

300+
TEST(CallerStreamGuardTest, ExplicitNullStreamIsStillSelected) {
301+
CallerStreamGuard guard(nullptr);
302+
ASSERT_TRUE(getCallerStream().has_value());
303+
EXPECT_EQ(*getCallerStream(), nullptr);
304+
}
305+
306+
TEST(CallerStreamGuardTest, SelectionIsThreadLocal) {
307+
const cudaStream_t selected = fake_stream(0);
308+
CallerStreamGuard guard(selected);
309+
310+
bool child_started_without_stream = false;
311+
bool child_selected_own_stream = false;
312+
std::thread child([&]() {
313+
child_started_without_stream = !getCallerStream().has_value();
314+
CallerStreamGuard child_guard(fake_stream(1));
315+
child_selected_own_stream = getCallerStream() == fake_stream(1);
316+
});
317+
child.join();
318+
319+
EXPECT_TRUE(child_started_without_stream);
320+
EXPECT_TRUE(child_selected_own_stream);
321+
EXPECT_EQ(getCallerStream(), selected);
322+
}
323+
299324
TEST(CallerStreamGuardTest, NestedGuardsRestoreOuter) {
300325
const cudaStream_t outer = fake_stream(1);
301326
const cudaStream_t inner = fake_stream(2);

0 commit comments

Comments
 (0)