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
206 changes: 104 additions & 102 deletions docs/SETTINGS.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions engine/include/strata/core/expert_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ struct GpuPlanSink {
int32_t cpu_jobs = 0; ///< host only: the distinct experts the pool computes for the layer just published
float* hit_out_dev = nullptr; ///< device address of this group's hit_out buffer on the primary GPU (P2P peer scatter)
bool device_hits_only = false; ///< STRATA_VERIFY_DEVICE_HITS=1: GPU built its own hit plan; host only marks kind[]
uint32_t want_flag = 0; ///< direct GPU-to-GPU: flag value to signal upon peer completion
bool peer_signals_flag = false; ///< direct GPU-to-GPU: set when peer GPU directly signals the flag over PCIe
};

/// Verifier::set_pcie_balance: a verify window's costs, fitted to each layer's measured times (ms, least squares with
Expand Down
31 changes: 30 additions & 1 deletion engine/include/strata/core/peer_experts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ namespace strata::core {
void set_peer_portable(bool on);
bool peer_portable();

struct PeerPlanDevice {
unsigned long long ptr[64];
int32_t start[68];
int32_t count[4];
int32_t dst[64];
int32_t tok[64];
int32_t out_row[64];
};

class PeerExperts {
public:
PeerExperts() = default;
Expand Down Expand Up @@ -64,7 +73,7 @@ class PeerExperts {
/// multi-GPU: with `out` (pinned, portable host rows) the peer writes its rows straight into
/// out[entry] with a scatter kernel (no D2H copy + host memcpy in finish); STRATA_PEER_DIRECT=0: the old way.
bool launch(int64_t layer, const float* x, const int32_t* ids, int64_t n_tok, int64_t k, const int32_t* kind,
std::string& err, float* out = nullptr);
std::string& err, float* out = nullptr, uint32_t want_flag = 0);
/// Waits for the launched share and writes its rows into `out` (row i = entry i, n_embd floats).
bool finish(float* out, std::string& err);

Expand Down Expand Up @@ -92,6 +101,13 @@ class PeerExperts {
/// whether a launch given host rows writes them itself (STRATA_PEER_DIRECT, default on): then the caller need not
/// zero a peer row at all, whether the launch has happened yet or not
static bool direct_mode();
void set_target_flag(volatile uint32_t* h_flag);
bool signaled_target_flag() const { return signaled_target_flag_; }
bool bind_verifier_buffers(int par, volatile uint32_t* h_seq, volatile uint32_t* h_flag,
const float* h_x, const int32_t* h_ids, float* h_ymiss);
bool launch_window(int par, int64_t lb, int64_t le, int T, int k, uint32_t want_flag_start, int G = 1);
bool autonomous() const { return autonomous_; }
void set_autonomous(bool on) { autonomous_ = on; }
double gib() const { return cache_.gib(); }
int64_t entries() const { return entries_; } ///< routed entries the peer computed
int64_t experts() const { return experts_; } ///< distinct (layer, expert) the peer computed
Expand Down Expand Up @@ -131,7 +147,20 @@ class PeerExperts {
volatile uint32_t* h_done_seq_ = nullptr;
uint32_t* d_done_seq_ = nullptr;
int* d_blocks_finished_ = nullptr;
volatile uint32_t* h_target_flag_ = nullptr;
uint32_t* d_target_flag_ = nullptr;
bool signaled_target_flag_ = false;
uint32_t seq_ = 0;
bool autonomous_ = false;
uint32_t* d_seq_[2] = {nullptr, nullptr};
uint32_t* d_target_flag_p_[2] = {nullptr, nullptr};
const float* d_m_x_[2] = {nullptr, nullptr};
const int32_t* d_m_ids_[2] = {nullptr, nullptr};
float* d_m_ymiss_[2] = {nullptr, nullptr};
int32_t* d_res_peer_ = nullptr;
unsigned long long* d_slot_off_peer_ = nullptr;
PeerPlanDevice* d_plans_ = nullptr;
int* d_blocks_finished_arr_ = nullptr;
};

} // namespace strata::core
12 changes: 12 additions & 0 deletions engine/include/strata/core/verify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

namespace strata::core {

class PeerExperts;

/// 0.1.43 (STRATA_ADAPT_OVERLAP): the adaptive tier's copies still landing while a decode window runs. The window's
/// residency tables are the baseline's (the swapped-in experts are resident from the window's first layer), but their
/// bytes arrive layer by layer: `run` holds a layer's plan (the first thing that lets the GPU read the layer's expert
Expand Down Expand Up @@ -100,6 +102,12 @@ class Verifier {
Verifier(const Verifier&) = delete;
Verifier& operator=(const Verifier&) = delete;
void set_remote_expert_opt(RemoteExpertOpt* opt) { remote_opt_ = opt; } // before init/capture
uint32_t* h_seq() const { return h_seq_; }
float* h_x() const { return h_x_; }
int32_t* h_ids() const { return h_ids_; }
float* h_ymiss() const { return h_ymiss_; }
void set_peer(PeerExperts* peer, int parity = 0);
bool peer_autonomous() const { return peer_autonomous_; }

/// The watchdog's view of the window in flight (issue #31): the layer, the GPU's sequence, the flags.
void diag(std::FILE* f) const;
Expand Down Expand Up @@ -421,6 +429,7 @@ class Verifier {
/// The GPU plan the pool writes each layer (VRAM hits + the PCIe share of the misses); give it to the
/// dispatch (`ExpertDispatch::plan`) before the first `run`.
GpuPlanSink* plan_sink() { return &sink_; }
uint32_t* h_flag() const { return h_flag_; }
/// Plan v0.3 P6: split the window into two token groups and pipeline the CPU experts of one with the GPU work
/// of the other (default on). Set before the first `run`.
void set_split(bool on) { split_ = on; }
Expand Down Expand Up @@ -771,6 +780,9 @@ class Verifier {
std::function<bool(cudaStream_t, std::string&)> pre_launch_; // set_pre_launch
int groups_[strata::kernels::kVerifyLongT + 1] = {};
float* h_ymiss_ = nullptr; float* m_ymiss_ = nullptr; // T * k * n_embd
PeerExperts* peer_ = nullptr;
int parity_ = 0;
bool peer_autonomous_ = false;

// device
void* arena_ = nullptr;
Expand Down
11 changes: 10 additions & 1 deletion engine/include/strata/kernels/elementwise.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,16 @@ void copy_from_mapped(float* dst, const float* src, int64_t n, void* stream);
/// multi-GPU: dst[rows[r] * width ..] = src[r * width ..] for r < n (float4; dst may be mapped host memory).
void scatter_rows_f32(const float* src, float* dst, const int32_t* rows, int64_t n, int64_t width, void* stream);
void scatter_rows_and_signal_f32(const float* src, float* dst, const int32_t* rows, int64_t n, int64_t width,
uint32_t* d_done_ptr, uint32_t val, int* d_blocks_finished, void* stream);
uint32_t* d_done_ptr, uint32_t val, int* d_blocks_finished, void* stream,
uint32_t* d_flag_ptr = nullptr, uint32_t flag_val = 0);
void scatter_rows_and_signal_fixed_f32(const float* src, float* dst, const int32_t* rows,
const int32_t* d_count, int64_t cap_entries, int64_t width,
int* d_blocks_finished, void* stream,
uint32_t* d_flag_ptr, uint32_t flag_val);
void peer_plan(const int32_t* ids, int n, int k, const int32_t* res, int n_expert,
const uint8_t* cache_base, const unsigned long long* slot_off, long long blob,
unsigned long long* grp_ptr, int32_t* grp_start, int32_t* grp_count,
int32_t* grp_dst, int32_t* grp_tok, int32_t* grp_out_row, void* stream);
void signal_stream_done(uint32_t* d_ptr, uint32_t val, void* stream);
void copy_rows_from_mapped(float* dst, const float* src, int64_t rows, int64_t width, const int32_t* hit_rows,
const int32_t* count, void* stream);
Expand Down
3 changes: 2 additions & 1 deletion engine/include/strata/kernels/verify_kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ void copy_rows_strided(float* dst, const float* src, int64_t rows, int64_t w, in
/// left empty and *plan_err (mapped host memory) is set to 1, so the host sees it instead of running a stale plan.
void resident_plan(const int32_t* ids, int n_entries, int k, const int32_t* res_layer, int n_expert,
const uint8_t* cache_base, const unsigned long long* slot_off, long long blob, int32_t* plan,
long long capx, uint32_t* skip, uint32_t ring, void* stream, uint32_t* plan_err = nullptr);
long long capx, uint32_t* skip, uint32_t ring, void* stream, uint32_t* plan_err = nullptr,
bool allow_partial = false);
/// Partial-VRAM on-device hit plan (`STRATA_VERIFY_DEVICE_HITS=1`, `--pcie-frac 0`, CUDA / HIP): groups only the
/// resident routed experts (`res_layer[e] >= 0`) in first-occurrence routing order into `plan` (`counts = {hit_groups,
/// hit_entries, 0}`), matching `expert_pool_dispatch_multi` when `fetches == 0` without waiting on `m_flagA_`/`m_flagB_`.
Expand Down
34 changes: 21 additions & 13 deletions engine/src/core/expert_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5702,18 +5702,38 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32
: (e >= 0 && e < d.n_expert && d.peer != nullptr && d.peer->has(d.layers, e)) ? 2 : -1;
}
}
static const bool dec_batch = [] { const char* v = std::getenv("STRATA_DEC_BATCH"); return v == nullptr || std::atoi(v) != 0; }();
// the GPU's copy_rows_from_mapped zeroes the rows it computed itself (dec_batch); STRATA_VERIFY_DEVICE_PLAN /
// STRATA_VERIFY_DEVICE_HITS's copy_or_zero_from_mapped copies every row, so there the host still zeroes them
static const bool device_plan = [] {
const char* p = std::getenv("STRATA_VERIFY_DEVICE_PLAN");
const char* h = std::getenv("STRATA_VERIFY_DEVICE_HITS");
return (p != nullptr && std::atoi(p) != 0) || (h != nullptr && std::atoi(h) != 0);
}();
const bool gpu_zeroes_hits = (d.plan != nullptr && n <= kMaxWindowEntries && n <= d.plan->cap && dec_batch && !device_plan);
bool any_cpu = false;
bool any_tail = false;
for (int64_t i = 0; i < n; ++i) {
if (kind[i] == -1) any_cpu = true; // -2: skipped (STRATA_ROUTE_TAIL_SKIP), host zeroes row below
else if (kind[i] == -2) any_tail = true;
}
const bool can_peer_signal = (!any_cpu && !any_tail && gpu_zeroes_hits && d.remote_count == 0 && d.plan != nullptr && d.plan->want_flag > 0);
const uint32_t peer_want = can_peer_signal ? d.plan->want_flag : 0;
if (d.peer != nullptr) { // multi-GPU: start the second GPU's share before the CPU's own work
std::string perr;
float* peer_out = (d.plan != nullptr && d.peer->p2p() && d.plan->hit_out_dev != nullptr && d.peer->direct_mode())
? d.plan->hit_out_dev
: out;
if (!d.peer->launch(d.layers, x_f, ids, n_tok, k, kind, perr, peer_out)) {
if (!d.peer->launch(d.layers, x_f, ids, n_tok, k, kind, perr, peer_out, peer_want)) {
std::fprintf(stderr, "strata: %s (layer %lld)\n", perr.c_str(), (long long) d.layers);
d.failed = true;
d.fail = "the peer GPU's experts could not be launched";
d.fail_layer = d.layers;
return;
}
if (d.plan != nullptr && d.peer->signaled_target_flag()) {
d.plan->peer_signals_flag = true;
}
}
if (d.remote_count > 0) {
static thread_local std::string remote_error;
Expand All @@ -5724,18 +5744,6 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32
for (int64_t i = 0; i < n; ++i) if (d.remote[r]->owns(i)) kind[i] = 2;
}
}
static const bool dec_batch = [] { const char* v = std::getenv("STRATA_DEC_BATCH"); return v == nullptr || std::atoi(v) != 0; }();
// the GPU's copy_rows_from_mapped zeroes the rows it computed itself (dec_batch); STRATA_VERIFY_DEVICE_PLAN /
// STRATA_VERIFY_DEVICE_HITS's copy_or_zero_from_mapped copies every row, so there the host still zeroes them
static const bool device_plan = [] {
const char* p = std::getenv("STRATA_VERIFY_DEVICE_PLAN");
const char* h = std::getenv("STRATA_VERIFY_DEVICE_HITS");
return (p != nullptr && std::atoi(p) != 0) || (h != nullptr && std::atoi(h) != 0);
}();
const bool gpu_zeroes_hits = (d.plan != nullptr && n <= kMaxWindowEntries && n <= d.plan->cap && dec_batch && !device_plan);
bool any_cpu = false;
for (int64_t i = 0; i < n; ++i)
if (kind[i] == -1) { any_cpu = true; break; } // -2: skipped (STRATA_ROUTE_TAIL_SKIP), no work
if (d.dres_check != nullptr && any_cpu) { // STRATA_ADAPT_CHECKRES
bool published = false;
for (int64_t i = 0; i < n && !published; ++i)
Expand Down
Loading
Loading