Skip to content
Closed
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
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,12 @@ if (BUILD_TESTING)
add_test(NAME tp_executor_test COMMAND tp_executor_test)
set_tests_properties(tp_executor_test PROPERTIES LABELS "cpu;tp2" TIMEOUT 60)

add_executable(tp_snapshot_gate_test tests/cli/tp_snapshot_gate_test.cpp)
target_link_libraries(tp_snapshot_gate_test PRIVATE Threads::Threads)
add_test(NAME tp_snapshot_gate_test COMMAND tp_snapshot_gate_test)
set_tests_properties(tp_snapshot_gate_test PROPERTIES
LABELS "cpu;tp2" TIMEOUT 20)

add_executable(json_constraint_test tests/core/json_constraint_test.cpp)
target_link_libraries(json_constraint_test PRIVATE gufo_core)
target_compile_definitions(json_constraint_test PRIVATE
Expand Down
2 changes: 1 addition & 1 deletion cmake/Checks.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ set(gufo_pr_targets
text_generation_scheduler_test
component_text_cache_test text_model_runner_test continuation_disk_store_test
bench_cli_test prompt_cli_test eval_http_client_test tp_control_test
tp_disk_store_test
tp_disk_store_test tp_snapshot_gate_test
qwen_tokenizer_test qwen_chat_template_test
qwen27b_continuation_layout_test
qwen38_flash_next_config_test qwen38_flash_next_mtp_sampling_test
Expand Down
5 changes: 5 additions & 0 deletions src/cli/serve/tp_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,8 @@ void TpMirroredRunner::Advance(TextRunnerState& state,
TextDecodeStep TpMirroredRunner::DecodeStep(
TextRunnerState& state, std::size_t max_tokens,
sampling::SamplerState& sampler) const {
if (multi_token_decode_ && max_tokens >= 2)
snapshot_gate_.Wait();
const std::lock_guard<std::recursive_mutex> call_lock(call_mutex_);
// A multi-token cycle is one instruction. It carries the sampler's draw
// state, so rank 1, whose sampler otherwise matches, draws what rank 0 draws
Expand Down Expand Up @@ -983,6 +985,7 @@ std::vector<TextDecodeStep> TpMirroredRunner::DecodeBatch(
// an unmirrored constraint one token at a time.
return TextModelRunner::DecodeBatch(decodes);
}
snapshot_gate_.Wait();
const std::lock_guard<std::recursive_mutex> call_lock(call_mutex_);
// One instruction carries every member's budget and draw state, like the
// decode each replaces, and the batch's draft count: rank 0 chooses it from
Expand Down Expand Up @@ -1227,7 +1230,9 @@ std::unique_ptr<TextRunnerSnapshot> TpMirroredRunner::SnapshotForPersistence(

std::unique_ptr<TextRunnerSnapshot> TpMirroredRunner::Capture(
const TextRunnerState& state, bool complete) const {
TpSnapshotGate::Pending pending(snapshot_gate_);
const std::lock_guard<std::recursive_mutex> call_lock(call_mutex_);
pending.Acquired();
const auto& mirrored = Mirrored(state);
if (next_snapshot_id_ == std::numeric_limits<std::uint64_t>::max()) {
throw std::overflow_error("TP snapshot ID exhausted");
Expand Down
2 changes: 2 additions & 0 deletions src/cli/serve/tp_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "src/cli/serve/text_model_runner.hpp"
#include "src/cli/serve/tp_control.hpp"
#include "src/cli/serve/tp_disk_store.hpp"
#include "src/cli/serve/tp_snapshot_gate.hpp"

namespace gufo::server {

Expand Down Expand Up @@ -306,6 +307,7 @@ class TpMirroredRunner final
std::shared_ptr<TpCallScope> scope_;
mutable std::uint64_t next_snapshot_id_{1};
mutable std::recursive_mutex call_mutex_;
mutable TpSnapshotGate snapshot_gate_;
bool multi_token_decode_{false};
mutable std::mutex mutex_;
mutable std::uint32_t next_state_id_{0};
Expand Down
48 changes: 48 additions & 0 deletions src/cli/serve/tp_snapshot_gate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef GUFO_SERVER_TP_SNAPSHOT_GATE_HPP_
#define GUFO_SERVER_TP_SNAPSHOT_GATE_HPP_

#include <atomic>
#include <cstdint>
#include <utility>

namespace gufo::server {

// Decode waits outside the existing recursive call mutex, allowing an already
// waiting snapshot to acquire it before another multi-token decode cycle.
// This does not preempt an active call or impose ordering on prefill.
class TpSnapshotGate {
public:
class Pending {
public:
explicit Pending(TpSnapshotGate& gate) noexcept : gate_(&gate) {
gate_->pending_.fetch_add(1, std::memory_order_acq_rel);
}
Pending(const Pending&) = delete;
Pending& operator=(const Pending&) = delete;
~Pending() { Acquired(); }

void Acquired() noexcept {
if (auto* gate = std::exchange(gate_, nullptr)) {
gate->pending_.fetch_sub(1, std::memory_order_release);
gate->pending_.notify_all();
}
}

private:
TpSnapshotGate* gate_;
};

void Wait() const noexcept {
auto pending = pending_.load(std::memory_order_acquire);
while (pending != 0) {
pending_.wait(pending, std::memory_order_acquire);
pending = pending_.load(std::memory_order_acquire);
}
}

private:
std::atomic<std::uint32_t> pending_{0};
};

} // namespace gufo::server
#endif
100 changes: 100 additions & 0 deletions tests/cli/tp_snapshot_gate_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include "src/cli/serve/tp_snapshot_gate.hpp"

#include <atomic>
#include <cassert>
#include <chrono>
#include <future>
#include <iostream>
#include <latch>
#include <mutex>
#include <thread>
#include <vector>

using gufo::server::TpSnapshotGate;
using namespace std::chrono_literals;

int main() {
// Pending captures hold decode at the boundary until every reservation
// clears.
for (bool abandon : {false, true}) {
TpSnapshotGate gate;
std::future<void> decoder;
std::latch entered(1);
{
TpSnapshotGate::Pending first(gate);
TpSnapshotGate::Pending second(gate);
decoder = std::async(std::launch::async, [&] {
entered.count_down();
gate.Wait();
});
entered.wait();
assert(decoder.wait_for(20ms) == std::future_status::timeout);
first.Acquired();
first.Acquired(); // Idempotence must not release another reservation.
assert(decoder.wait_for(20ms) == std::future_status::timeout);
if (!abandon)
second.Acquired();
}
assert(decoder.wait_for(2s) == std::future_status::ready);
decoder.get();
}
// Mirror the production recursive lock: nested Advance remains legal while a
// capture waits, then a subsequent decode yields outside that same mutex.
for (int round = 0; round != 100; ++round) {
TpSnapshotGate gate;
std::recursive_mutex calls;
std::latch registered(1);
std::vector<int> order;
std::unique_lock active(calls);
auto capture = std::async(std::launch::async, [&] {
TpSnapshotGate::Pending pending(gate);
registered.count_down();
std::lock_guard lock(calls);
pending.Acquired();
order.push_back(2);
});
registered.wait();
{
std::lock_guard nested(calls);
order.push_back(1);
}
auto next = std::async(std::launch::async, [&] {
gate.Wait();
std::lock_guard lock(calls);
order.push_back(3);
});
active.unlock();
assert(capture.wait_for(2s) == std::future_status::ready);
assert(next.wait_for(2s) == std::future_status::ready);
capture.get();
next.get();
assert((order == std::vector<int>{1, 2, 3}));
}
// Concurrent reservations and decodes finish without losing registrations.
TpSnapshotGate gate;
std::recursive_mutex calls;
std::atomic<int> snapshots{0}, decodes{0};
std::vector<std::thread> workers;
for (int worker = 0; worker != 6; ++worker) {
workers.emplace_back([&, worker] {
for (int i = 0; i != 2000; ++i) {
if (worker < 3) {
TpSnapshotGate::Pending pending(gate);
std::lock_guard lock(calls);
pending.Acquired();
++snapshots;
} else {
gate.Wait();
std::lock_guard lock(calls);
++decodes;
}
}
});
}
for (auto& worker : workers)
worker.join();
assert(snapshots == 6000 && decodes == 6000);
gate.Wait();
std::cout << "PASS: multiple reservations, abandonment, idempotence, "
"recursive lock handoff, concurrent completion\n";
}