Skip to content
Open
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 .ci/scripts/muse_glimmer_batching_workload.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ class EvidenceExecutor final : public batching::Executor {
bool initialize() override {
return inner_.initialize();
}
bool on_idle() override {
return inner_.on_idle();
}
std::size_t preferred_batch_tokens() const override {
return inner_.preferred_batch_tokens();
}
Expand Down
14 changes: 14 additions & 0 deletions .ci/scripts/tests/muse_glimmer_batching_workload_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ void rejected(F action) {
class FakeExecutor final : public batching::Executor {
public:
bool succeeds = true;
int idle_calls = 0;
bool on_idle() override {
++idle_calls;
return succeeds;
}
std::optional<batching::SessionId> open_session() override {
return 1;
}
Expand Down Expand Up @@ -96,10 +101,19 @@ void successful_batch_owners() {
FakeExecutor inner;
ci::EvidenceExecutor executor(inner);
batching::BatchOutput out;
require(executor.on_idle());
require(inner.idle_calls == 1);
inner.succeeds = false;
require(!executor.on_idle());
require(inner.idle_calls == 2);
// Virtual forwarding consumes this setting before it is reassigned.
// cppcheck-suppress redundantAssignment
inner.succeeds = true;
require(executor.execute({{input(1), input(1)}}, out));
require(executor.execute({{input(2)}}, out));
require(executor.evidence().max_sessions == 1);
rejected([&] { ci::check_batching(executor.evidence()); });
// cppcheck-suppress redundantAssignment
inner.succeeds = false;
require(!executor.execute({{input(1), input(2)}}, out));
require(executor.evidence().multi_session_steps == 0);
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/cuda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,9 @@ jobs:
export ET_MUSE_DFLASH_TINY_DIR="${RUNNER_TEMP:-/tmp}/muse_glimmer_dflash_tiny"
python examples/models/muse-glimmer/tests/export_cuda_dflash_tiny.py \
--output-dir "${ET_MUSE_DFLASH_TINY_DIR}"
export ET_MUSE_DFLASH_SEQUENCE_TINY_DIR="${RUNNER_TEMP:-/tmp}/muse_glimmer_dflash_sequence_tiny"
python examples/models/muse-glimmer/tests/export_cuda_dflash_tiny.py \
--output-dir "${ET_MUSE_DFLASH_SEQUENCE_TINY_DIR}" --kv-layout sequence
ctest --test-dir "${BATCHING_BIN_DIR}" -R '^muse_glimmer_cuda_dflash$' \
--output-on-failure --no-tests=error -V > /tmp/muse_glimmer_cuda_dflash.log 2>&1 \
|| { cat /tmp/muse_glimmer_cuda_dflash.log; exit 1; }
Expand Down
49 changes: 45 additions & 4 deletions backends/cuda/batching/cuda_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ CudaExecutor::CudaExecutor(
std::vector<ForwardMethod> methods,
int min_selected_rows,
bool has_sampler,
bool has_argmax)
bool has_argmax,
std::optional<std::chrono::milliseconds> column_compaction_delay)
: install_guard_(cache),
module_(std::move(module)),
ctl_(cache->as<llm_cache::BatchControl>()),
Expand All @@ -281,7 +282,8 @@ CudaExecutor::CudaExecutor(
min_selected_rows_(min_selected_rows),
sessions_(*ctl_, max_sessions, max_session_tokens, vocab_size),
has_sampler_(has_sampler),
has_argmax_(has_argmax) {}
has_argmax_(has_argmax),
column_compaction_delay_(column_compaction_delay) {}

CudaExecutor::~CudaExecutor() {
if (device_params_ != nullptr) {
Expand Down Expand Up @@ -312,7 +314,8 @@ Result<std::unique_ptr<CudaExecutor>> CudaExecutor::create(
int max_session_tokens,
int kv_dtype,
int initial_capacity,
CudaExecutorOptions options) {
CudaExecutorOptions options,
std::optional<std::chrono::milliseconds> column_compaction_delay) {
ET_CHECK_OR_RETURN_ERROR(
max_session_tokens > 0,
InvalidArgument,
Expand All @@ -323,6 +326,7 @@ Result<std::unique_ptr<CudaExecutor>> CudaExecutor::create(
config.kv_dtype = kv_dtype;
config.initial_capacity = initial_capacity;
config.options = std::move(options);
config.kv_column_compaction_delay = column_compaction_delay;
return create(std::move(module), config);
}

Expand All @@ -334,6 +338,11 @@ Result<std::unique_ptr<CudaExecutor>> CudaExecutor::create(
const int kv_dtype = config.kv_dtype;
const int initial_capacity = config.initial_capacity;
const auto& options = config.options;
ET_CHECK_OR_RETURN_ERROR(
!config.kv_column_compaction_delay ||
config.kv_column_compaction_delay->count() >= 0,
InvalidArgument,
"CudaExecutor: negative compaction delay");
ET_CHECK_OR_RETURN_ERROR(
module != nullptr, InvalidArgument, "CudaExecutor: no program");
ET_CHECK_OR_RETURN_ERROR(
Expand Down Expand Up @@ -588,7 +597,8 @@ Result<std::unique_ptr<CudaExecutor>> CudaExecutor::create(
std::move(methods),
min_selected_rows,
has_sampler,
has_argmax));
has_argmax,
config.kv_column_compaction_delay));
executor->embeddings_method_ = embeddings_method;
return executor;
}
Expand Down Expand Up @@ -1093,6 +1103,37 @@ CudaGraphMetrics CudaExecutor::graph_metrics(std::string_view method) const {
return kv_->graph_metrics(method);
}

Error CudaExecutor::compact_kv_columns(int spare_columns) {
ET_CHECK_OR_RETURN_ERROR(
!raw_executing_, InvalidState, "CudaExecutor: compact between executes");
return kv_->compact_columns(spare_columns, std::chrono::milliseconds{0});
}

bool CudaExecutor::on_idle() {
if (!column_compaction_delay_ || raw_executing_) {
return true;
}
const auto metrics = kv_->metrics();
// Below 25% non-scratch occupancy, retain spare room equal to the live count.
// Time controls when this runs, never which owners can be removed.
if (metrics.physical_columns <= 2 ||
4 * metrics.live_sequences + 1 >= metrics.physical_columns) {
return true;
}
const Error error = kv_->compact_columns(
static_cast<int>(metrics.live_sequences), *column_compaction_delay_);
if (error == Error::NotSupported) {
return true; // Fixed physical columns need no idle maintenance.
}
if (error != Error::Ok) {
ET_LOG(
Error,
"CudaExecutor: idle KV compaction failed (%d)",
static_cast<int>(error));
}
return error == Error::Ok;
}

std::vector<ForwardMethodCalls> CudaExecutor::method_calls() const {
std::vector<ForwardMethodCalls> calls;
calls.reserve(methods_.size() + 1);
Expand Down
23 changes: 20 additions & 3 deletions backends/cuda/batching/cuda_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// runs the method with the smallest width that holds it; a static method's
// extra rows are padding.

#include <chrono>
#include <cstddef>
#include <cstdint>
#include <functional>
Expand Down Expand Up @@ -88,6 +89,13 @@ struct CudaExecutorConfig {
int max_session_tokens = 0;
int kv_dtype = static_cast<int>(::executorch::aten::ScalarType::BFloat16);
int initial_capacity = -1;
// Idle compaction starts below 25% occupancy of non-scratch columns and keeps
// spare columns equal to the live-owner count, retaining at least one
// non-scratch column when none are live. Open sessions never expire.
// nullopt disables automatic compaction; zero permits the next idle check.
// A later burst can regrow columns and recapture CUDA graphs.
std::optional<std::chrono::milliseconds> kv_column_compaction_delay{
std::chrono::milliseconds{500}};
CudaExecutorOptions options;
};

Expand Down Expand Up @@ -158,13 +166,16 @@ class ET_EXPERIMENTAL CudaExecutor : public llm_batching::Executor {
// reserves `max_session_tokens` cells; together they must fit the program's
// max_cells. `kv_dtype` is the ET ScalarType K/V is stored in; a negative
// `initial_capacity` starts the pools at the cache's default.
// `column_compaction_delay` controls idle maintenance; nullopt disables it.
static ::executorch::runtime::Result<std::unique_ptr<CudaExecutor>> create(
std::unique_ptr<::executorch::extension::Module> module,
int max_sessions,
int max_session_tokens,
int kv_dtype,
int initial_capacity = -1,
CudaExecutorOptions options = {});
CudaExecutorOptions options = {},
std::optional<std::chrono::milliseconds> column_compaction_delay =
std::chrono::milliseconds{500});

// The widest method's step. A wider batch runs as several forwards.
std::size_t preferred_batch_tokens() const override {
Expand Down Expand Up @@ -230,6 +241,10 @@ class ET_EXPERIMENTAL CudaExecutor : public llm_batching::Executor {
OffGraphKVMetrics kv_metrics() const;
// Sequence-layout counters; read between executes, on the engine thread.
CudaGraphMetrics graph_metrics(std::string_view method) const;
// Call on the engine thread between executes, never concurrently with the
// Runner. Explicit compaction preserves inactive but still-open owners.
::executorch::runtime::Error compact_kv_columns(int spare_columns = 1);
bool on_idle() override;
// Every method, narrowest first, with how many forwards it ran. Read it
// between executes, not while one runs.
std::vector<ForwardMethodCalls> method_calls() const;
Expand All @@ -254,7 +269,8 @@ class ET_EXPERIMENTAL CudaExecutor : public llm_batching::Executor {
std::vector<ForwardMethod> methods,
int min_selected_rows,
bool has_sampler,
bool has_argmax);
bool has_argmax,
std::optional<std::chrono::milliseconds> column_compaction_delay);

// Draws one token per row of `logits`, rows [0, sessions.size()), on the
// device; returns them in row order.
Expand All @@ -275,7 +291,7 @@ class ET_EXPERIMENTAL CudaExecutor : public llm_batching::Executor {
llm_cache::InstallGuard install_guard_;
std::unique_ptr<::executorch::extension::Module> module_;
llm_cache::BatchControl* const ctl_;
const CudaKVCache* const kv_;
CudaKVCache* const kv_;
std::string backend_id_;
std::int32_t vocab_size_;
int max_session_tokens_;
Expand Down Expand Up @@ -303,6 +319,7 @@ class ET_EXPERIMENTAL CudaExecutor : public llm_batching::Executor {
std::optional<bool> device_logits_;
bool initialized_ = false;
bool raw_executing_ = false;
const std::optional<std::chrono::milliseconds> column_compaction_delay_;
};

} // namespace executorch::backends::cuda::batching
Loading
Loading