Files
FreeToken/tests/moe/hip_probe_faults.cpp
ZihaoMuandLZ-QWQ 9cacd0896c fix(moe): publish rocm cpu expert inputs before notification (#764)
* fix(moe): prevent stale cpu expert inputs in rocm graphs

* fix(moe): publish rocm cpu inputs with graph events

* fix(moe): qualify rocm kernel io with ordered copy fallback

* fix(moe): enable locked hybrid staging on rocm

---------

Co-authored-by: LZ-QWQ <lz1830837889@163.com>
2026-11-02 21:29:42 -08:00

84 lines
3.1 KiB
C++

#include <hip/hip_runtime.h>
#include <dlfcn.h>
#include <atomic>
#include <cstdlib>
#include <cstring>
namespace {
std::atomic<int> begins{0}, ends{0}, injected{0}, removed_nodes{0}, last_end_rc{0};
bool mode(const char* value) {
const char* selected = std::getenv("FREETOKEN_TEST_HIP_PROBE_FAULT");
return selected != nullptr && std::strcmp(selected, value) == 0;
}
template <typename Function>
Function real(const char* name) {
return reinterpret_cast<Function>(dlsym(RTLD_NEXT, name));
}
} // namespace
extern "C" hipError_t hipStreamBeginCapture(hipStream_t stream, hipStreamCaptureMode capture_mode) {
const hipError_t rc = real<decltype(&hipStreamBeginCapture)>("hipStreamBeginCapture")(stream, capture_mode);
if (rc == hipSuccess) ++begins;
return rc;
}
extern "C" hipError_t hipStreamWriteValue64(hipStream_t stream, void* address, uint64_t value, unsigned flags) {
// Force the compatibility path on runtimes that normally record native writes.
if (mode("dependency") && begins == 1) {
++injected;
return hipSuccess;
}
return real<decltype(&hipStreamWriteValue64)>("hipStreamWriteValue64")(stream, address, value, flags);
}
extern "C" hipError_t hipEventRecordWithFlags(hipEvent_t event, hipStream_t stream, unsigned flags) {
if (mode("publication-event") && flags == hipEventRecordExternal) {
++injected;
return hipErrorNotSupported;
}
return real<decltype(&hipEventRecordWithFlags)>("hipEventRecordWithFlags")(event, stream, flags);
}
extern "C" hipError_t hipStreamUpdateCaptureDependencies(
hipStream_t stream, hipGraphNode_t* dependencies, size_t count, unsigned flags) {
if (mode("dependency")) {
++injected;
return hipErrorNotSupported;
}
return real<decltype(&hipStreamUpdateCaptureDependencies)>("hipStreamUpdateCaptureDependencies")(
stream, dependencies, count, flags);
}
extern "C" hipError_t hipGraphDestroyNode(hipGraphNode_t node) {
const hipError_t rc = real<decltype(&hipGraphDestroyNode)>("hipGraphDestroyNode")(node);
if (rc == hipSuccess) ++removed_nodes;
return rc;
}
extern "C" hipError_t hipStreamEndCapture(hipStream_t stream, hipGraph_t* graph) {
++ends;
hipError_t rc = real<decltype(&hipStreamEndCapture)>("hipStreamEndCapture")(stream, graph);
if (mode("end-capture") && ends == 2 && rc == hipSuccess) {
// A real invalid API call also sets HIP's thread-local last-error state.
rc = real<decltype(&hipGraphGetNodes)>("hipGraphGetNodes")(*graph, nullptr, nullptr);
++injected;
}
last_end_rc = static_cast<int>(rc);
return rc;
}
extern "C" int probe_test_injected() { return injected; }
extern "C" int probe_test_begins() { return begins; }
extern "C" int probe_test_ends() { return ends; }
extern "C" int probe_test_removed_nodes() { return removed_nodes; }
extern "C" int probe_test_last_end_rc() { return last_end_rc; }
extern "C" int probe_test_peek_error() {
return static_cast<int>(real<decltype(&hipPeekAtLastError)>("hipPeekAtLastError")());
}
extern "C" int probe_test_make_error() {
return static_cast<int>(real<decltype(&hipGraphGetNodes)>("hipGraphGetNodes")(nullptr, nullptr, nullptr));
}