mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
loop: the looped-core code is compiled only into the q4_lite build (STRATA_WITH_LOOP)
Every hunk that #7673 and the Loop lane added to the shared engine sources (verify, prefill, session, generate, conversation cache / spill / snapshot / file / state, shared_expert, the Metal stub list) is wrapped in #ifdef STRATA_WITH_LOOP <new> #else <old> #endif, so a build without the macro compiles exactly the text f-0226-q4-ggufc3fefe0caahad (checked mechanically: the macro-off text of every file equalsc3fefe0caa, the macro-on text equals the unguarded file). cmake/strata_geometry.cmake defines STRATA_WITH_LOOP for -DSTRATA_GEOMETRY=q4_lite only; loop_adapter.cu, its cuBLAS link, loop_adapter_test and the stub sources are added only then. Flash-Next, Qwen3.6, Ling, Hy4, Inkling, the Metal and the SYCL builds are unchanged. Not guarded: the hip_compat cublasSgemm alias (a macro nobody expands), the Python server flags (inert without --loop-adapter), docs.
This commit is contained in:
1 parent
59d682c535
commit
1b0e6a2ba5
20 files changed
+1021
-9
No files matched your search
@@ -232,11 +232,13 @@ if(STRATA_BUILD_CONVERSATION_TESTS)
|
||||
add_executable(session_save_reclaim_test src/core/session_save_reclaim_test.cpp src/core/conversation_file.cpp)
|
||||
target_include_directories(session_save_reclaim_test PRIVATE include)
|
||||
add_test(NAME session_save_reclaim_test COMMAND session_save_reclaim_test)
|
||||
add_executable(loop_adapter_test src/kernels/loop_adapter_test.cpp src/core/conversation_file.cpp)
|
||||
target_include_directories(loop_adapter_test PRIVATE include)
|
||||
target_compile_features(loop_adapter_test PRIVATE cxx_std_17)
|
||||
target_link_libraries(loop_adapter_test PRIVATE Threads::Threads)
|
||||
add_test(NAME loop_adapter_test COMMAND loop_adapter_test)
|
||||
if(STRATA_WITH_LOOP_BUILD)
|
||||
add_executable(loop_adapter_test src/kernels/loop_adapter_test.cpp src/core/conversation_file.cpp)
|
||||
target_include_directories(loop_adapter_test PRIVATE include)
|
||||
target_compile_features(loop_adapter_test PRIVATE cxx_std_17)
|
||||
target_link_libraries(loop_adapter_test PRIVATE Threads::Threads)
|
||||
add_test(NAME loop_adapter_test COMMAND loop_adapter_test)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(STRATA_HIP_GFX906)
|
||||
|
||||
@@ -33,6 +33,15 @@ elseif(NOT STRATA_GEOMETRY STREQUAL "flash_next")
|
||||
set(_strata_geo_label "${STRATA_GEOMETRY}")
|
||||
endif()
|
||||
|
||||
# The looped-core adapter (--loop-adapter, the owner's Qwen4-35B-A3B-Loop.safetensors, community #7673) is compiled into the q4_lite shape only:
|
||||
# every other build (Flash-Next, Qwen3.6, Ling, Hy4, Inkling, SYCL ...) compiles the text it had before the adapter (STRATA_WITH_LOOP undefined).
|
||||
set(STRATA_WITH_LOOP_BUILD OFF)
|
||||
if(NOT STRATA_GEOMETRY_FILE AND STRATA_GEOMETRY STREQUAL "q4_lite")
|
||||
set(STRATA_WITH_LOOP_BUILD ON)
|
||||
add_compile_definitions(STRATA_WITH_LOOP=1)
|
||||
message(STATUS "strata: looped-core adapter compiled in (STRATA_WITH_LOOP)")
|
||||
endif()
|
||||
|
||||
if(_strata_geo_header)
|
||||
set(_strata_geo_out "${CMAKE_BINARY_DIR}/strata_geometry")
|
||||
file(MAKE_DIRECTORY "${_strata_geo_out}/strata/core")
|
||||
|
||||
@@ -89,7 +89,6 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)
|
||||
src/kernels/cuda/kv_q4.cu
|
||||
src/kernels/cuda/fused_gr.cu
|
||||
src/kernels/cuda/cvec.cu
|
||||
src/kernels/cuda/loop_adapter.cu
|
||||
src/kernels/cuda/qsa_decode_attn.cu
|
||||
src/kernels/cuda/qsa_prompt_attn.cu
|
||||
src/kernels/cuda/fused_gdn.cu
|
||||
@@ -121,7 +120,11 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)
|
||||
COMPILE_OPTIONS "-mavx;-mno-avx2;-mno-fma;-mno-f16c")
|
||||
endif()
|
||||
endif()
|
||||
target_link_libraries(strata_kernels PUBLIC strata_core strata_artifact ${_strata_gpu_blas_target})
|
||||
target_link_libraries(strata_kernels PUBLIC strata_core strata_artifact)
|
||||
if(STRATA_WITH_LOOP_BUILD) # the looped-core adapter (q4_lite only): its kernels and cuBLAS
|
||||
target_sources(strata_kernels PRIVATE src/kernels/cuda/loop_adapter.cu)
|
||||
target_link_libraries(strata_kernels PUBLIC ${_strata_gpu_blas_target})
|
||||
endif()
|
||||
# Exact gfx1100 prompt scorer (PR1825 method port): optional standalone test,
|
||||
# also available where the published source omits tests/.
|
||||
option(STRATA_PARITY_GFX1100_QSA_SCORES "Build the exact gfx1100 QSA score/ID check" OFF)
|
||||
|
||||
@@ -106,8 +106,10 @@ struct ConversationKvReuse {
|
||||
// The conversation the image was captured from (the first stage's only): a park reuses it only for the prefix it
|
||||
// shares with the conversation then in the main session (a batch slot may have replaced that session's K/V since).
|
||||
std::vector<int32_t> ids;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// Loop execution mode compatibility key (0 when --loop-adapter is off; prevents cross-mode K/V reuse).
|
||||
uint64_t loop_key = 0;
|
||||
#endif
|
||||
size_t bytes() const {
|
||||
size_t n = kv.capacity() * sizeof(ConversationKv) + stages.capacity() * sizeof(ConversationKvReuse) +
|
||||
ids.capacity() * sizeof(int32_t);
|
||||
@@ -219,7 +221,9 @@ struct SavedConversation {
|
||||
std::vector<ConversationCheckpoint> checkpoints;
|
||||
std::vector<ConversationKv> kv; // main layers followed by the draft layer
|
||||
bool cvec = true;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
uint64_t loop_key = 0; // looped adapter execution mode key (0 = stock, 1 = T=1 bypass, >=2 = looped config hash)
|
||||
#endif
|
||||
// with a layer split, the later stages' own images, one per stage, in stage order
|
||||
std::vector<SavedConversation> stage_images;
|
||||
// STRATA_PARK_ASYNC: set while a background copy fills this image's K/V; its deleter waits for that copy, and as
|
||||
@@ -337,11 +341,20 @@ public:
|
||||
// `stage_kv`: with a layer split, the later stages' restored K/V (one per stage), retained with the first's.
|
||||
// `ids`: the conversation the K/V belongs to (take_reuse_for refuses it for any other; none: never reused).
|
||||
void retain(std::vector<ConversationKv>&& kv, int64_t tokens,
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
std::vector<std::vector<ConversationKv>>&& stage_kv = {}, std::vector<int32_t> ids = {},
|
||||
uint64_t loop_key = 0) {
|
||||
#else
|
||||
std::vector<std::vector<ConversationKv>>&& stage_kv = {}, std::vector<int32_t> ids = {}) {
|
||||
#endif
|
||||
reuse_ = {};
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
ConversationKvReuse candidate{std::move(kv), tokens, tokens, {}, std::move(ids), loop_key};
|
||||
for (auto& k : stage_kv) candidate.stages.push_back(ConversationKvReuse{std::move(k), tokens, tokens, {}, {}, loop_key});
|
||||
#else
|
||||
ConversationKvReuse candidate{std::move(kv), tokens, tokens, {}, std::move(ids)};
|
||||
for (auto& k : stage_kv) candidate.stages.push_back(ConversationKvReuse{std::move(k), tokens, tokens, {}, {}});
|
||||
#endif
|
||||
if (enabled() && candidate.bytes() <= budget_ - bytes_) reuse_ = std::move(candidate);
|
||||
}
|
||||
void limit_reuse(int64_t first_dirty) {
|
||||
@@ -354,9 +367,15 @@ public:
|
||||
// from, and nothing when they share none. Without this a park after a batch slot gave its conversation back to the
|
||||
// main session wrote the RESTORED conversation's K/V into the new one's image (#1503, a cross-conversation leak).
|
||||
template<class Token>
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
ConversationKvReuse take_reuse_for(const std::vector<Token>& live, uint64_t loop_key = 0) {
|
||||
#else
|
||||
ConversationKvReuse take_reuse_for(const std::vector<Token>& live) {
|
||||
#endif
|
||||
ConversationKvReuse r = std::exchange(reuse_, {});
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (r.loop_key != loop_key) return {};
|
||||
#endif
|
||||
const int64_t n = std::min<int64_t>((int64_t) r.ids.size(), (int64_t) live.size());
|
||||
int64_t same = 0;
|
||||
while (same < n && (int64_t) r.ids[(size_t) same] == (int64_t) live[(size_t) same]) ++same;
|
||||
@@ -369,8 +388,13 @@ public:
|
||||
int64_t retained_tokens() const { return reuse_.kv.empty() ? 0 : reuse_.unchanged_tokens; }
|
||||
// take_reuse_for can use a positive prefix exactly when the first tokens match.
|
||||
template<class Token>
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
bool has_reuse_for(const std::vector<Token>& live, uint64_t loop_key = 0) const {
|
||||
return retained_tokens() > 0 && reuse_.loop_key == loop_key && !reuse_.ids.empty() && !live.empty() &&
|
||||
#else
|
||||
bool has_reuse_for(const std::vector<Token>& live) const {
|
||||
return retained_tokens() > 0 && !reuse_.ids.empty() && !live.empty() &&
|
||||
#endif
|
||||
(int64_t) reuse_.ids.front() == (int64_t) live.front();
|
||||
}
|
||||
bool can_fit(size_t incoming, size_t held = 0) const {
|
||||
@@ -379,14 +403,22 @@ public:
|
||||
}
|
||||
|
||||
template<class Token>
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
Match best(const std::vector<Token>& prompt, const std::vector<ConversationImageKey>& images, bool cvec,
|
||||
uint64_t loop_key = 0) const {
|
||||
#else
|
||||
Match best(const std::vector<Token>& prompt, const std::vector<ConversationImageKey>& images, bool cvec) const {
|
||||
#endif
|
||||
Match best;
|
||||
// Ties prefer the most recently parked branch. The caller prefers its
|
||||
// already-active state when that offers the same prefix length.
|
||||
for (size_t i = entries_.size(); i-- > 0;) {
|
||||
const auto& e = entries_[i];
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (e.cvec != cvec || e.loop_key != loop_key) continue;
|
||||
#else
|
||||
if (e.cvec != cvec) continue;
|
||||
#endif
|
||||
auto consider = [&](const ConversationCheckpoint& c, bool live) {
|
||||
const int64_t n = conversation_prefix(c, prompt, images);
|
||||
if (n > best.tokens) best = {i, n, live, serials_[i]};
|
||||
@@ -400,10 +432,18 @@ public:
|
||||
/// A parked conversation holds this state already (its live tokens or one of its checkpoints start with `ids`,
|
||||
/// same pictures, same steering): see conversation_covers. Read-only; no LRU effect.
|
||||
template<class Token>
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
bool holds(const std::vector<Token>& ids, const std::vector<ConversationImageKey>& images, bool cvec,
|
||||
uint64_t loop_key = 0) const {
|
||||
#else
|
||||
bool holds(const std::vector<Token>& ids, const std::vector<ConversationImageKey>& images, bool cvec) const {
|
||||
#endif
|
||||
for (const auto& e : entries_) {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (e.cvec != cvec || e.loop_key != loop_key) continue;
|
||||
#else
|
||||
if (e.cvec != cvec) continue;
|
||||
#endif
|
||||
if (conversation_covers(e.live, ids, images)) return true;
|
||||
for (const auto& c : e.checkpoints)
|
||||
if (conversation_covers(c, ids, images)) return true;
|
||||
@@ -562,7 +602,11 @@ public:
|
||||
}
|
||||
size_t drop_superseded(const std::vector<int32_t>& ids, const std::vector<ConversationImageKey>& images,
|
||||
const std::vector<ConversationCheckpoint>& checkpoints, bool cvec,
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
std::vector<size_t>* dropped_tokens = nullptr, uint64_t loop_key = 0) {
|
||||
#else
|
||||
std::vector<size_t>* dropped_tokens = nullptr) {
|
||||
#endif
|
||||
auto held = [&](const ConversationCheckpoint& c) {
|
||||
if (c.ids == ids && c.imgs == images) return true;
|
||||
for (const auto& k : checkpoints)
|
||||
@@ -575,7 +619,11 @@ public:
|
||||
const ConversationCheckpoint* deepest = nullptr;
|
||||
for (const auto& c : e.checkpoints)
|
||||
if (!deepest || c.ids.size() > deepest->ids.size()) deepest = &c;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (e.cvec == cvec && e.loop_key == loop_key && !(guarded_ != 0 && serials_[i] == guarded_) &&
|
||||
#else
|
||||
if (e.cvec == cvec && !(guarded_ != 0 && serials_[i] == guarded_) &&
|
||||
#endif
|
||||
((deepest && !deepest->ids.empty() && held(*deepest)) || live_extended_by(e, ids, images))) {
|
||||
if (dropped_tokens) dropped_tokens->push_back(e.live.ids.size());
|
||||
bytes_ -= e.bytes();
|
||||
@@ -595,8 +643,12 @@ public:
|
||||
// for a 5 K-token prefix). True when drop_superseded(ids, images, checkpoints, cvec) would drop an entry that has a checkpoint the
|
||||
// given chain does not hold. Nothing is changed.
|
||||
bool supersedes_richer(const std::vector<int32_t>& ids, const std::vector<ConversationImageKey>& images,
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const std::vector<ConversationCheckpoint>& checkpoints, bool cvec,
|
||||
uint64_t loop_key = 0) const {
|
||||
#else
|
||||
const std::vector<ConversationCheckpoint>& checkpoints, bool cvec) const {
|
||||
#endif
|
||||
auto held = [&](const ConversationCheckpoint& c) {
|
||||
if (c.ids == ids && c.imgs == images) return true;
|
||||
for (const auto& k : checkpoints)
|
||||
@@ -608,7 +660,11 @@ public:
|
||||
const ConversationCheckpoint* deepest = nullptr;
|
||||
for (const auto& c : e.checkpoints)
|
||||
if (!deepest || c.ids.size() > deepest->ids.size()) deepest = &c;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (e.cvec != cvec || e.loop_key != loop_key || !deepest || deepest->ids.empty() || !held(*deepest) ||
|
||||
#else
|
||||
if (e.cvec != cvec || !deepest || deepest->ids.empty() || !held(*deepest) ||
|
||||
#endif
|
||||
(guarded_ != 0 && serials_[i] == guarded_))
|
||||
continue;
|
||||
for (const auto& c : e.checkpoints)
|
||||
@@ -622,11 +678,19 @@ public:
|
||||
/// park now would only rewrite what a turn from now drops: the prefix such an entry holds is this conversation's
|
||||
/// prefix, and the conversation only appends, so the copy stays usable for the reuse it was made for.
|
||||
size_t parked_prefix_tokens(const std::vector<int32_t>& ids, const std::vector<ConversationImageKey>& images,
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const std::vector<ConversationCheckpoint>& checkpoints, bool cvec,
|
||||
uint64_t loop_key = 0) const {
|
||||
#else
|
||||
const std::vector<ConversationCheckpoint>& checkpoints, bool cvec) const {
|
||||
#endif
|
||||
size_t longest = 0;
|
||||
for (const auto& e : entries_) {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (e.cvec != cvec || e.loop_key != loop_key) continue;
|
||||
#else
|
||||
if (e.cvec != cvec) continue;
|
||||
#endif
|
||||
const ConversationCheckpoint* deepest = nullptr;
|
||||
for (const auto& c : e.checkpoints)
|
||||
if (!deepest || c.ids.size() > deepest->ids.size()) deepest = &c;
|
||||
@@ -642,7 +706,11 @@ public:
|
||||
bool put(SavedConversation&& image, size_t held = 0) {
|
||||
const size_t n = image.bytes();
|
||||
if (!enabled() || held > budget_ || n > budget_ - held) return false; // make_room's refusal, first
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
drop_superseded(image.live.ids, image.live.imgs, image.checkpoints, image.cvec, nullptr, image.loop_key);
|
||||
#else
|
||||
drop_superseded(image.live.ids, image.live.imgs, image.checkpoints, image.cvec);
|
||||
#endif
|
||||
if (!make_room(n, held)) return false;
|
||||
entries_.push_back(std::move(image));
|
||||
serials_.push_back(next_serial_++);
|
||||
@@ -669,7 +737,11 @@ public:
|
||||
image = std::move(staged);
|
||||
throw;
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
drop_superseded(staged.live.ids, staged.live.imgs, staged.checkpoints, staged.cvec, nullptr, staged.loop_key);
|
||||
#else
|
||||
drop_superseded(staged.live.ids, staged.live.imgs, staged.checkpoints, staged.cvec);
|
||||
#endif
|
||||
entries_.back() = std::move(staged);
|
||||
++next_serial_;
|
||||
bytes_ += n;
|
||||
|
||||
@@ -121,7 +121,9 @@ struct ConversationView {
|
||||
const std::vector<ConversationImageKey>& images;
|
||||
const std::vector<ConversationCheckpoint>& checkpoints;
|
||||
bool cvec;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
uint64_t loop_key = 0;
|
||||
#endif
|
||||
};
|
||||
bool conversation_snapshot_bytes(const ConversationView& view, const SessionState& session,
|
||||
const ModelGeometry& g, const QsaState& draft, size_t& bytes, std::string& error);
|
||||
|
||||
@@ -276,7 +276,9 @@ public:
|
||||
std::filesystem::path file;
|
||||
uint64_t bytes = 0;
|
||||
bool cvec = false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
uint64_t loop_key = 0;
|
||||
#endif
|
||||
uint64_t used = 0; // last use (written, matched-and-read): the least recently used goes first
|
||||
ConversationCheckpoint live; // ids and images only
|
||||
std::vector<ConversationCheckpoint> checkpoints; // ids and images only
|
||||
@@ -344,7 +346,11 @@ public:
|
||||
}
|
||||
Entry e;
|
||||
if (ok) {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
e.serial = serial; e.file = f; e.bytes = written; e.cvec = image.cvec; e.loop_key = image.loop_key;
|
||||
#else
|
||||
e.serial = serial; e.file = f; e.bytes = written; e.cvec = image.cvec;
|
||||
#endif
|
||||
e.live = stub(image.live);
|
||||
for (const auto& c : image.checkpoints) e.checkpoints.push_back(stub(c));
|
||||
}
|
||||
@@ -371,13 +377,21 @@ public:
|
||||
/// The entry and the prefix it gives `prompt`, by the RAM cache's rule (ConversationCache::best): its live state or a
|
||||
/// checkpoint, same pictures, same steering; ties prefer the newest. Reach it by `serial` (find / take_serial).
|
||||
template <class Token>
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
ConversationCache::Match best(const std::vector<Token>& prompt, const std::vector<ConversationImageKey>& images,
|
||||
bool cvec, uint64_t loop_key = 0) const {
|
||||
#else
|
||||
ConversationCache::Match best(const std::vector<Token>& prompt, const std::vector<ConversationImageKey>& images, bool cvec) const {
|
||||
#endif
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
ConversationCache::Match m;
|
||||
for (size_t i = entries_.size(); i-- > 0;) {
|
||||
const Entry& e = entries_[i];
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (e.cvec != cvec || e.loop_key != loop_key) continue;
|
||||
#else
|
||||
if (e.cvec != cvec) continue;
|
||||
#endif
|
||||
auto consider = [&](const ConversationCheckpoint& c, bool live) {
|
||||
const int64_t n = conversation_prefix(c, prompt, images);
|
||||
if (n > m.tokens) m = {i, n, live, e.serial};
|
||||
@@ -442,13 +456,19 @@ public:
|
||||
/// A snapshot of the same conversation is being parked, grown past these tokens: deletes the entries (file and index file)
|
||||
/// whose live tokens are a prefix of `image_live` (same steering and pictures) - they would never be read again. Returns the
|
||||
/// token counts of the deleted entries.
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
std::vector<size_t> drop_superseded(const std::vector<int32_t>& ids, const std::vector<ConversationImageKey>& imgs,
|
||||
bool cvec, uint64_t loop_key = 0) {
|
||||
#else
|
||||
std::vector<size_t> drop_superseded(const std::vector<int32_t>& ids, const std::vector<ConversationImageKey>& imgs, bool cvec) {
|
||||
#endif
|
||||
SavedConversation image_live; // (tokens and pictures only)
|
||||
image_live.live.ids = ids;
|
||||
image_live.live.imgs = imgs;
|
||||
image_live.cvec = cvec;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
image_live.loop_key = loop_key;
|
||||
#endif
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
std::vector<size_t> out;
|
||||
for (size_t i = entries_.size(); i-- > 0;)
|
||||
@@ -487,7 +507,11 @@ private:
|
||||
/// `e` is a prefix (equal or shorter) of `image`'s live tokens, same steering and pictures: `image` serves every resume `e` could
|
||||
static bool superseded_by(const Entry& e, const SavedConversation& image) {
|
||||
const size_t n = e.live.ids.size();
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (n == 0 || e.cvec != image.cvec || e.loop_key != image.loop_key || n > image.live.ids.size() ||
|
||||
#else
|
||||
if (n == 0 || e.cvec != image.cvec || n > image.live.ids.size() ||
|
||||
#endif
|
||||
!std::equal(e.live.ids.begin(), e.live.ids.end(), image.live.ids.begin())) return false;
|
||||
for (const auto& k : e.live.imgs) { // its pictures are the first ones of the image's
|
||||
bool found = false;
|
||||
@@ -508,11 +532,19 @@ private:
|
||||
for (const auto& k : c.imgs) { put_pod(b, k.start); put_pod(b, k.hash); }
|
||||
}
|
||||
static bool meta_write(const std::filesystem::path& f, const Entry& e, const SessionFileIdentity& id) {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
std::string b = e.loop_key != 0 ? "SPM2" : "SPM1";
|
||||
#else
|
||||
std::string b = "SPM1";
|
||||
#endif
|
||||
put_pod(b, id.model); put_pod(b, id.config);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
put_pod(b, (uint8_t) e.cvec);
|
||||
if (e.loop_key != 0) put_pod(b, e.loop_key);
|
||||
put_pod(b, e.bytes);
|
||||
#else
|
||||
put_pod(b, (uint8_t) e.cvec); put_pod(b, e.bytes);
|
||||
#endif
|
||||
put_ckpt(b, e.live);
|
||||
put_pod(b, (uint64_t) e.checkpoints.size());
|
||||
for (const auto& c : e.checkpoints) put_ckpt(b, c);
|
||||
@@ -579,13 +611,19 @@ private:
|
||||
std::fclose(fp);
|
||||
} else good = false;
|
||||
Rd r{b};
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool is_v2 = b.size() >= 4 && b.compare(0, 4, "SPM2") == 0;
|
||||
good = good && (is_v2 || (b.size() >= 4 && b.compare(0, 4, "SPM1") == 0));
|
||||
#else
|
||||
good = good && b.compare(0, 4, "SPM1") == 0;
|
||||
#endif
|
||||
if (good) {
|
||||
r.at = 4;
|
||||
const uint64_t model = r.pod<uint64_t>(), config = r.pod<uint64_t>();
|
||||
f.e.cvec = r.pod<uint8_t>() != 0;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
f.e.loop_key = is_v2 ? r.pod<uint64_t>() : 0;
|
||||
#endif
|
||||
f.e.bytes = r.pod<uint64_t>();
|
||||
good = !r.bad && model == id.model && config == id.config && fs::file_size(sess, e2) == f.e.bytes && !e2 &&
|
||||
get_ckpt(r, f.e.live, f.e.bytes);
|
||||
@@ -639,10 +677,18 @@ private:
|
||||
/// steering): for the images the background writer still holds in RAM. {tokens, live}; tokens 0 = none.
|
||||
template <class Token>
|
||||
std::pair<int64_t, bool> conversation_image_prefix(const SavedConversation& s, const std::vector<Token>& prompt,
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const std::vector<ConversationImageKey>& images, bool cvec,
|
||||
uint64_t loop_key = 0) {
|
||||
#else
|
||||
const std::vector<ConversationImageKey>& images, bool cvec) {
|
||||
#endif
|
||||
std::pair<int64_t, bool> m{0, false};
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (s.cvec != cvec || s.loop_key != loop_key) return m;
|
||||
#else
|
||||
if (s.cvec != cvec) return m;
|
||||
#endif
|
||||
const int64_t l = conversation_prefix(s.live, prompt, images);
|
||||
if (l > m.first) m = {l, true};
|
||||
for (const auto& c : s.checkpoints) {
|
||||
|
||||
@@ -27,7 +27,9 @@
|
||||
#include "strata/core/flag_order.hpp"
|
||||
#include "strata/core/layer.hpp"
|
||||
#include "strata/core/session.hpp"
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
#include "strata/kernels/loop_adapter.hpp"
|
||||
#endif
|
||||
#include "strata/kernels/sampler.hpp"
|
||||
#include "strata/kernels/window_limits.hpp"
|
||||
#include "strata/kernels/verify_kernels.hpp"
|
||||
@@ -234,6 +236,7 @@ public:
|
||||
void set_next(Verifier* next, void* next_user) { next_ = next; next_user_ = next_user; }
|
||||
/// floats per token in a hand-off buffer
|
||||
static int64_t handoff_floats(const ModelGeometry& g) { return (int64_t) g.hc * g.n_embd + g.n_embd + g.hc; }
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
/// floats per token in a cyclic split's hand-off: the residual hand-off, then a relayed m_prev (hc x n_embd)
|
||||
static int64_t cyclic_handoff_floats(const ModelGeometry& g) { return handoff_floats(g) + (int64_t) g.hc * g.n_embd; }
|
||||
|
||||
@@ -267,6 +270,7 @@ public:
|
||||
|
||||
/// Update the committed `<think>`-span tracking state on this stage and any successors without changing `passes`.
|
||||
void set_loop_cd_in_thinking(bool in_thinking);
|
||||
#endif
|
||||
|
||||
/// Keep the first `n_keep` (1..T) tokens of the last window; advances `ss.ple_prev` by them.
|
||||
bool commit(int n_keep, std::string& err);
|
||||
@@ -675,6 +679,7 @@ private:
|
||||
bool chain_overlap() const; ///< this stage hands off through a flag to a next stage that waits on it
|
||||
Verifier* next_ = nullptr;
|
||||
void* next_user_ = nullptr;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// set_cyclic: this stage's visits, their hand-offs, the shared itinerary, the visit being recorded / run, and
|
||||
// the window graphs of the visits not loaded into exec_ / exec_nr_ (cyc_select swaps them in)
|
||||
struct CycGraphs {
|
||||
@@ -706,6 +711,7 @@ private:
|
||||
bool loop_cd_in_thinking_ = false;
|
||||
float* h_cd_omega_ = nullptr;
|
||||
float* m_cd_omega_ = nullptr;
|
||||
#endif
|
||||
bool ple_stage() const { return lb_ <= 1 && 1 < le_; } ///< holds layer 1, where the PLE block runs
|
||||
void stage_inputs(int T, const int32_t* tokens, int64_t pos0);
|
||||
bool staged_ = false;
|
||||
@@ -829,7 +835,11 @@ private:
|
||||
bool commit_pending_ = false;
|
||||
cudaStream_t copy_ = nullptr; // the copy engine's stream (DMA of missed experts)
|
||||
using FlagSet = strata::core::FlagRaise;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
FlagSet flag_sets_[2 * 160 * 2] = {}; // host-function arguments, one per (step, group)
|
||||
#else
|
||||
FlagSet flag_sets_[2 * 64 * 2] = {}; // host-function arguments, one per (layer, group)
|
||||
#endif
|
||||
static void fetch_dma(void* ctx, const uint8_t* const* src, int n, size_t bytes);
|
||||
static void raise_flag(uint32_t* flag, uint32_t value);
|
||||
/// Flag B for `want`, behind every copy queued on copy_ (an empty plan must not overtake an earlier layer's DMA).
|
||||
@@ -853,9 +863,11 @@ private:
|
||||
void* arena_ = nullptr;
|
||||
int32_t *tok_ = nullptr, *step_ = nullptr, *pos_ = nullptr, *commit_ = nullptr;
|
||||
float *ple_ = nullptr, *emb_ = nullptr, *R_ = nullptr, *mixed_ = nullptr, *bo_ = nullptr;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
float *R_anchor_ = nullptr, *R_mprev_ = nullptr, *R_m0_ = nullptr; // Looped adapter residual checkpoints
|
||||
float *ab_alpha_ = nullptr, *ab_beta_ = nullptr; // Looped adapter GDN a/b LoRA scratch
|
||||
float *cd_omega_ = nullptr; // Looped adapter per-token LoopCD omega [T]
|
||||
#endif
|
||||
float *inj_ = nullptr, *inj2_ = nullptr, *lo_ = nullptr, *rs_ = nullptr, *xn_ = nullptr;
|
||||
uint8_t* xq_ = nullptr; // T columns of q8_1
|
||||
uint8_t* xil_ = nullptr; // fork F4: the interleaved copy of xq_'s 2-4 columns
|
||||
|
||||
@@ -81,7 +81,11 @@ void shared_expert(const uint8_t* x_q8_0, const uint8_t* x_q8k, const uint16_t*
|
||||
void shared_expert_multi(int n_tok, const float* x, const uint16_t* x_bf16, const NativeSharedWeights& nw,
|
||||
const uint16_t* gate_inp_bf16, float* gate, float* up, float* g, float* out, int64_t n_embd,
|
||||
int64_t n_ff, void* stream, const void* x_q8_1_ready = nullptr,
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
int lfuse = 0, int loop_pass = -1, int64_t loop_layer = -1); // S26 STRATA_LFUSE + Looped adapter LoRA
|
||||
#else
|
||||
int lfuse = 0); // S26 STRATA_LFUSE: bit 0 gate computed + applied by the caller, bit 1 gate/up pair
|
||||
#endif
|
||||
|
||||
/// The MoE block's final combination, `ref/moe.py::moe` L156:
|
||||
///
|
||||
|
||||
@@ -26,7 +26,9 @@
|
||||
|
||||
namespace strata::core { class PeerExperts; }
|
||||
namespace strata::kernels::cpu { class ExpertPool; }
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
namespace strata::kernels { struct LoopVisit; }
|
||||
#endif
|
||||
|
||||
namespace strata::prefill {
|
||||
|
||||
@@ -290,6 +292,7 @@ public:
|
||||
stage_lb_ = layer_begin; stage_le_ = layer_end; next_ = next;
|
||||
}
|
||||
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
/// CYCLIC LAYER SPLIT (a looped adapter with a stage boundary inside its recurrent core): `paths[s]` is stage s's
|
||||
/// prompt path (0 = the first) and `stage_begin[s]` its first layer. A prompt's chunks then run the itinerary of
|
||||
/// kernels::build_loop_itinerary: each stage reads its visits of a chunk in order, on a thread of its own, once the
|
||||
@@ -304,6 +307,7 @@ public:
|
||||
/// `in_thinking` at `pos0`). No-op when `--loop-adapter` is not loaded.
|
||||
void set_loop_request(int passes, float cd_omega, bool cd_thinking, bool in_thinking);
|
||||
|
||||
#endif
|
||||
/// LAYER SPLIT: `helper` is another stage's prompt path (another GPU). A prompt of one chunk runs the stages one
|
||||
/// after the other, so while this stage reads it the helper's GPU idles: it then streams a share of this stage's
|
||||
/// non-resident experts over its own PCIe link into its own (lent) prompt buffers, computes their rows, and sends
|
||||
@@ -415,6 +419,7 @@ private:
|
||||
std::future<bool> next_run_;
|
||||
int hand_buf_ = 0;
|
||||
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
struct Cyc; ///< set_cyclic: the itinerary and a run's progress (every stage's)
|
||||
std::shared_ptr<Cyc> cyc_;
|
||||
int cyc_stage_ = 0;
|
||||
@@ -430,6 +435,7 @@ private:
|
||||
bool loop_cd_in_thinking_ = false; ///< thinking-span state at pos0 of this run/chunk
|
||||
bool run_cyclic(const int64_t* tokens, int64_t n, int64_t pos0, std::string& err);
|
||||
|
||||
#endif
|
||||
bool carve(std::size_t T, void* alloc); // the device buffers of a chunk (prefill.cpp's Alloc)
|
||||
void release(); // the destructor's cleanup (also `reset`'s)
|
||||
struct Impl;
|
||||
|
||||
@@ -205,7 +205,7 @@ set(STRATA_METAL_HOST_SOURCES
|
||||
${S}/core/native_head.cpp ${S}/core/native_dense.cpp ${S}/core/verify.cpp ${S}/core/mtp.cpp
|
||||
${S}/core/conversation_snapshot.cpp ${S}/core/conversation_state.cpp ${S}/core/conversation_memory.cpp
|
||||
${S}/core/peer_experts.cpp ${S}/core/arch_defaults.cpp ${S}/core/vmm.cpp ${S}/core/conversation_file.cpp
|
||||
${S}/kernels/ngram.cpp ${S}/kernels/loop_adapter_stub.cpp ${S}/ngram/ple_reader.cpp ${S}/platform/direct_file.cpp ${S}/platform/io_priority.cpp
|
||||
${S}/kernels/ngram.cpp ${S}/ngram/ple_reader.cpp ${S}/platform/direct_file.cpp ${S}/platform/io_priority.cpp
|
||||
${S}/prefill/prefill.cpp
|
||||
${S}/spec/suffix_drafter.cpp ${S}/spec/controller.cpp ${S}/spec/draft_policy.cpp ${S}/spec/draft_source.cpp
|
||||
${S}/kernels/cpu/pool.cpp ${S}/kernels/cpu/expert_layout.cpp ${S}/kernels/cpu/native_expert.cpp
|
||||
@@ -217,6 +217,9 @@ set_source_files_properties(${S}/core/device.cu ${S}/core/pinned.cu
|
||||
# that stops the engine with the entry point's name (src/engine/stubs.cpp, generated by tools/gen_stubs.py).
|
||||
file(GLOB STRATA_METAL_ENGINE_SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/engine/*.cpp)
|
||||
|
||||
if(STRATA_WITH_LOOP_BUILD) # q4_lite only: generate.cpp names the loop adapter entry points; Metal has its own loop in src/engine/loop.inc
|
||||
list(APPEND STRATA_METAL_HOST_SOURCES ${S}/kernels/loop_adapter_stub.cpp)
|
||||
endif()
|
||||
add_library(strata_metal_host STATIC ${STRATA_METAL_HOST_SOURCES})
|
||||
target_include_directories(strata_metal_host PUBLIC ${STRATA_ROOT}/include ${S}/core ${STRATA_ROOT}/third_party/ggml)
|
||||
target_compile_definitions(strata_metal_host PUBLIC STRATA_NATIVE_EXPERTS=1 STRATA_HAVE_DFLASH2=1 STRATA_VERSION="0.1.73-metal")
|
||||
|
||||
@@ -321,7 +321,11 @@ namespace strata::kernels { void sample_tokens(const float * p0, int p1, int p2,
|
||||
namespace strata::kernels { void scale_inplace(float * p0, int64_t p1, float p2, void * p3) { strata::metal_engine::missing("strata::kernels::scale_inplace"); } }
|
||||
namespace strata::kernels { void scatter_rows_f32(const float * p0, float * p1, const int32_t * p2, int64_t p3, int64_t p4, void * p5) { strata::metal_engine::missing("strata::kernels::scatter_rows_f32"); } }
|
||||
namespace strata::kernels { void shared_expert(const uint8_t * p0, const uint8_t * p1, const uint16_t * p2, const SForm & p3, const uint8_t * p4, const float * p5, const float * p6, const SForm & p7, const uint8_t * p8, const float * p9, const float * p10, const SForm & p11, const uint8_t * p12, const float * p13, const float * p14, const uint16_t * p15, float * p16, float * p17, int64_t p18, int64_t p19, int p20, void * p21, const float * p22, const NativeSharedWeights * p23) { strata::metal_engine::missing("strata::kernels::shared_expert"); } }
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
namespace strata::kernels { void shared_expert_multi(int p0, const float * p1, const uint16_t * p2, const NativeSharedWeights & p3, const uint16_t * p4, float * p5, float * p6, float * p7, float * p8, int64_t p9, int64_t p10, void * p11, const void * p12, int p13, int p14, int64_t p15) { strata::metal_engine::missing("strata::kernels::shared_expert_multi"); } }
|
||||
#else
|
||||
namespace strata::kernels { void shared_expert_multi(int p0, const float * p1, const uint16_t * p2, const NativeSharedWeights & p3, const uint16_t * p4, float * p5, float * p6, float * p7, float * p8, int64_t p9, int64_t p10, void * p11, const void * p12, int p13) { strata::metal_engine::missing("strata::kernels::shared_expert_multi"); } }
|
||||
#endif
|
||||
namespace strata::kernels { void silu_inplace(float * p0, int64_t p1, void * p2) { strata::metal_engine::missing("strata::kernels::silu_inplace"); } }
|
||||
namespace strata::kernels { void spec_draft_sample(float * p0, int p1, const int32_t * p2, const int32_t * p3, int p4, const SamplerParams * p5, int32_t * p6, int p7, int p8, const int32_t * p9, void * p10, int32_t * p11, float * p12, int32_t * p13, int p14, float p15, void * p16) { strata::metal_engine::missing("strata::kernels::spec_draft_sample"); } }
|
||||
namespace strata::kernels { void topk_512_step(const float * p0, const QsaShapes & p1, int64_t p2, const int32_t * p3, int32_t * p4, void * p5) { strata::metal_engine::missing("strata::kernels::topk_512_step"); } }
|
||||
|
||||
@@ -692,14 +692,18 @@ void put_payload(Out& o, const SavedConversation& s, const std::vector<SessionKv
|
||||
put_checkpoint(o, s.live);
|
||||
o.u64(s.checkpoints.size());
|
||||
for (const auto& c : s.checkpoints) put_checkpoint(o, c);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
constexpr uint64_t kLoopKeyMagic = 0x4c4f4f504b455931ull; // "LOOPKEY1"
|
||||
#endif
|
||||
if (sources) {
|
||||
o.u64(sources->size());
|
||||
for (const auto& k : *sources) {
|
||||
put_kv_header(o, k.format, k.cells, k.heads, k.head_dim, k.page_size, k.pooled_rows, k.idx_dim);
|
||||
for (size_t part = 0; part < 5; ++part) o.source(k, part);
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (s.loop_key != 0) { o.u64(kLoopKeyMagic); o.u64(s.loop_key); }
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
o.u64(s.kv.size());
|
||||
@@ -707,7 +711,9 @@ void put_payload(Out& o, const SavedConversation& s, const std::vector<SessionKv
|
||||
put_kv_header(o, k.format, k.cells, k.heads, k.head_dim, k.page_size, k.pooled_rows, k.idx_dim);
|
||||
o.buffer(k.k); o.buffer(k.v); o.buffer(k.k_scale); o.buffer(k.v_scale); o.buffer(k.pooled);
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (s.loop_key != 0) { o.u64(kLoopKeyMagic); o.u64(s.loop_key); }
|
||||
#endif
|
||||
}
|
||||
|
||||
// ---- parsing: every count is checked against the bytes left and the caller's limits before anything is allocated
|
||||
@@ -795,6 +801,7 @@ bool get_payload(In& in, SavedConversation& s) {
|
||||
if (!in.buffer(k.k, 0) || !in.buffer(k.v, 1) || !in.buffer(k.k_scale, 2) || !in.buffer(k.v_scale, 3) ||
|
||||
!in.buffer(k.pooled, 4)) return false;
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
s.loop_key = 0;
|
||||
if (in.left == 16) {
|
||||
uint64_t magic = 0, lk = 0;
|
||||
@@ -802,6 +809,7 @@ bool get_payload(In& in, SavedConversation& s) {
|
||||
if (magic != 0x4c4f4f504b455931ull) return in.fail("invalid trailing payload bytes");
|
||||
s.loop_key = lk;
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
#include "conversation_checked.hpp"
|
||||
#include "conversation_copy.hpp"
|
||||
#include "conversation_trace.hpp"
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
#include "strata/kernels/loop_adapter.hpp"
|
||||
#endif
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
@@ -141,11 +143,16 @@ bool conversation_session_sizes(const ModelGeometry& g, const SessionState& ss,
|
||||
std::string& error) {
|
||||
if (!conversation_state_sizes(g, z, error)) return false;
|
||||
const int64_t n_gdn = g.n_gdn_layers(), n_qsa = g.n_qsa_layers();
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// a looped adapter's extra passes carve their own rows after the stage's base range
|
||||
const int64_t x_gdn = strata::kernels::loop_extra_gdn_slots(0, g.n_layers, g.qsa_interval);
|
||||
const int64_t x_qsa = strata::kernels::loop_extra_qsa_slots(0, g.n_layers, g.qsa_interval);
|
||||
if (ss.gdn_alloc < 0 || ss.gdn_alloc > n_gdn + x_gdn || ss.qsa_ord0 < 0 || ss.qsa_alloc < 0 ||
|
||||
ss.qsa_ord0 > n_qsa || ss.qsa_alloc > n_qsa + x_qsa - ss.qsa_ord0)
|
||||
#else
|
||||
if (ss.gdn_alloc < 0 || ss.gdn_alloc > n_gdn || ss.qsa_ord0 < 0 || ss.qsa_alloc < 0 ||
|
||||
ss.qsa_ord0 > n_qsa || ss.qsa_alloc > n_qsa - ss.qsa_ord0)
|
||||
#endif
|
||||
return fail(error, "invalid session layer carve");
|
||||
// z.gdn is n_gdn whole rows; the session holds gdn_alloc of them (all of them for the default full range)
|
||||
z.gdn = n_gdn > 0 ? z.gdn / (size_t) n_gdn * (size_t) ss.gdn_alloc : 0;
|
||||
@@ -592,8 +599,12 @@ bool conversation_snapshot_save(SavedConversation& image, const ConversationView
|
||||
captured.live.ids = view.ids; captured.live.imgs = view.images;
|
||||
const auto p1 = now();
|
||||
const long cf0 = ptr ? conversation_trace::thread_minflt() : 0;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
captured.cvec = view.cvec; captured.loop_key = view.loop_key;
|
||||
captured.checkpoints = conversation_detail::copy_checkpoints_beating(view.checkpoints, storage);
|
||||
#else
|
||||
captured.cvec = view.cvec; captured.checkpoints = conversation_detail::copy_checkpoints_beating(view.checkpoints, storage);
|
||||
#endif
|
||||
const long ck_faults = ptr ? conversation_trace::thread_minflt() - cf0 : 0;
|
||||
if (ptr) // (E1: where each copied checkpoint's running state went: faults of the whole copy, address, heap or mapping)
|
||||
for (const auto& c : captured.checkpoints)
|
||||
@@ -713,8 +724,12 @@ bool conversation_snapshot_sources(SavedConversation& meta, std::vector<SessionK
|
||||
captured.geometry = geometry_key(g);
|
||||
captured.layer_lo = ss.layer_lo; captured.layer_hi = ss.layer_hi;
|
||||
captured.live.ids = view.ids; captured.live.imgs = view.images;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
captured.cvec = view.cvec; captured.loop_key = view.loop_key;
|
||||
captured.checkpoints = conversation_detail::copy_checkpoints_beating(view.checkpoints);
|
||||
#else
|
||||
captured.cvec = view.cvec; captured.checkpoints = conversation_detail::copy_checkpoints_beating(view.checkpoints);
|
||||
#endif
|
||||
if (!conversation_checkpoint_save(captured.live, ss, g, error)) return false;
|
||||
// STRATA_CKPT_ASYNC: the caller writes `meta` to the session file at once, so the running state's copy lands first
|
||||
conversation_checkpoint_join(captured.live.gdn.data());
|
||||
@@ -770,7 +785,11 @@ bool conversation_snapshot_validate(const SavedConversation& image, const Sessio
|
||||
// an image holds exactly one carve's running state and K/V: same layer range or nothing
|
||||
if (image.layer_lo != ss.layer_lo || image.layer_hi != ss.layer_hi)
|
||||
return fail(error, "snapshot from another session layer range");
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const ConversationView view{image.live.ids, image.live.imgs, image.checkpoints, image.cvec, image.loop_key};
|
||||
#else
|
||||
const ConversationView view{image.live.ids, image.live.imgs, image.checkpoints, image.cvec};
|
||||
#endif
|
||||
if (!view_validate(view, ss, g, error) || !conversation_checkpoint_validate(image.live, ss, g, error)) return false;
|
||||
const size_t layers = owned_qsa(ss);
|
||||
if (image.kv.size() != layers + (draft ? 1 : 0)) return fail(error, "invalid K/V layer count");
|
||||
@@ -845,7 +864,9 @@ ConversationSwap conversation_snapshot_swap(ConversationSwapPlan& plan, SavedCon
|
||||
captured.geometry = geometry_key(g);
|
||||
captured.layer_lo = ss.layer_lo; captured.layer_hi = ss.layer_hi;
|
||||
captured.cvec = active.cvec;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
captured.loop_key = active.loop_key;
|
||||
#endif
|
||||
captured.live.ids = conversation_detail::copy_beating(active.ids);
|
||||
captured.live.imgs = conversation_detail::copy_beating(active.images);
|
||||
captured.checkpoints = conversation_detail::copy_checkpoints_beating(active.checkpoints);
|
||||
|
||||
@@ -5,7 +5,9 @@
|
||||
#include "strata/core/progress.hpp"
|
||||
|
||||
#include "strata/kernels/qsa.hpp"
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
#include "strata/kernels/loop_adapter.hpp"
|
||||
#endif
|
||||
#include "strata/kernels/elementwise.hpp"
|
||||
#include "strata/kernels/quantize_act.hpp"
|
||||
#include "strata/kernels/s2_expert_grouped.hpp"
|
||||
@@ -55,10 +57,15 @@ uint64_t session_bytes(const ModelGeometry& g, int64_t max_cells, int64_t k, int
|
||||
// QSA layers are `l % interval == interval-1`, so exactly `bound / interval` of them live below `bound`
|
||||
const int64_t I = std::max<int64_t>(g.qsa_interval, 1);
|
||||
const int64_t q_lo = layer_lo / I, q_hi = layer_hi / I;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const int64_t extra_q = strata::kernels::loop_extra_qsa_slots(layer_lo, layer_hi, I);
|
||||
const int64_t extra_g = strata::kernels::loop_extra_gdn_slots(layer_lo, layer_hi, I);
|
||||
const int64_t q_n = std::max<int64_t>(q_hi - q_lo, g.n_qsa_layers() > 0 ? 1 : 0) + extra_q;
|
||||
const int64_t gdn_n = std::max<int64_t>((layer_hi - layer_lo) - std::max<int64_t>(q_hi - q_lo, 0), 0) + extra_g;
|
||||
#else
|
||||
const int64_t q_n = std::max<int64_t>(q_hi - q_lo, g.n_qsa_layers() > 0 ? 1 : 0);
|
||||
const int64_t gdn_n = std::max<int64_t>((layer_hi - layer_lo) - std::max<int64_t>(q_hi - q_lo, 0), 0);
|
||||
#endif
|
||||
uint64_t n = 0;
|
||||
n += gdn_buffers_bytes(g);
|
||||
n += (uint64_t) gdn_n * gdn_state_floats(g) * 4;
|
||||
@@ -90,12 +97,22 @@ uint64_t session_init(const ModelGeometry& g, int64_t max_cells, int64_t k, void
|
||||
s.layer_hi = layer_hi;
|
||||
const int64_t I = std::max<int64_t>(g.qsa_interval, 1);
|
||||
const int64_t q_n_range = layer_hi / I - layer_lo / I;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const int64_t extra_q = strata::kernels::loop_extra_qsa_slots(layer_lo, layer_hi, I);
|
||||
const int64_t extra_g = strata::kernels::loop_extra_gdn_slots(layer_lo, layer_hi, I);
|
||||
#endif
|
||||
s.qsa_ord0 = layer_lo / I;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
s.qsa_alloc = std::max<int64_t>(q_n_range, g.n_qsa_layers() > 0 ? 1 : 0) + extra_q;
|
||||
#else
|
||||
s.qsa_alloc = std::max<int64_t>(q_n_range, g.n_qsa_layers() > 0 ? 1 : 0);
|
||||
#endif
|
||||
s.gdn_ord0 = layer_lo - layer_lo / I;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
s.gdn_alloc = std::max<int64_t>((layer_hi - layer_lo) - q_n_range, 0) + extra_g;
|
||||
#else
|
||||
s.gdn_alloc = std::max<int64_t>((layer_hi - layer_lo) - q_n_range, 0);
|
||||
#endif
|
||||
|
||||
gdn_buffers_init(g, take(gdn_buffers_bytes(g)), s.gdn);
|
||||
s.gdn_state = (float*) take((uint64_t) s.gdn_alloc * gdn_state_floats(g) * 4);
|
||||
@@ -107,8 +124,12 @@ uint64_t session_init(const ModelGeometry& g, int64_t max_cells, int64_t k, void
|
||||
// owns the RoPE table that the others - and the prefill staging identity, and the MTP drafter - borrow.
|
||||
const uint64_t first = qsa_state_bytes(g, max_cells, true), rest = qsa_state_bytes(g, max_cells, false);
|
||||
s.qsa_state_arena = take(g.n_qsa_layers() > 0 ? first + (uint64_t) (s.qsa_alloc - 1) * rest : 0);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const int64_t qsa_arr_n = std::max<int64_t>(g.n_qsa_layers() + extra_q, s.qsa_ord0 + s.qsa_alloc);
|
||||
s.qsa_states = new QsaState[(size_t) qsa_arr_n]();
|
||||
#else
|
||||
s.qsa_states = new QsaState[(size_t) g.n_qsa_layers()]();
|
||||
#endif
|
||||
s.qsa_buf_arena = take(qsa_buffers_bytes(g, max_cells));
|
||||
|
||||
uint8_t* qp = (uint8_t*) s.qsa_state_arena;
|
||||
|
||||
@@ -43,7 +43,9 @@
|
||||
#include "strata/kernels/cpu/hadamard.hpp"
|
||||
#include "strata/kernels/bf16_gemv.hpp"
|
||||
#include "strata/kernels/native_router.hpp"
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
#include "strata/kernels/router_top10.hpp"
|
||||
#endif
|
||||
#include "strata/kernels/native_moe.hpp"
|
||||
#include "strata/kernels/cpu/expert.hpp"
|
||||
#include "strata/kernels/elementwise.hpp"
|
||||
@@ -51,7 +53,9 @@
|
||||
#include "strata/kernels/fused_gr.hpp"
|
||||
#include "strata/kernels/route_prior.hpp"
|
||||
#include "strata/kernels/cvec.hpp"
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
#include "strata/kernels/loop_adapter.hpp"
|
||||
#endif
|
||||
#include "strata/kernels/gr.hpp"
|
||||
#include "strata/kernels/kv_q4.hpp"
|
||||
#include "strata/kernels/kv_q8.hpp"
|
||||
@@ -786,13 +790,16 @@ Verifier::~Verifier() {
|
||||
}
|
||||
if (cs_) cudaStreamSynchronize(cs_);
|
||||
if (sh_cs_) cudaStreamSynchronize(sh_cs_);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (bypass_loaded_) select_loop_bypass(false);
|
||||
#endif
|
||||
for (auto& row : exec_)
|
||||
for (auto& e : row)
|
||||
if (e) cudaGraphExecDestroy(e);
|
||||
for (auto& row : exec_nr_)
|
||||
for (auto& e : row)
|
||||
if (e) cudaGraphExecDestroy(e);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
for (size_t v = 0; v < cyc_graphs_.size(); ++v) { // (the loaded visit's graphs are exec_ / exec_nr_ above)
|
||||
if ((int) v == cyc_loaded_) continue;
|
||||
for (auto* tab : {&cyc_graphs_[v].exec, &cyc_graphs_[v].exec_nr})
|
||||
@@ -804,8 +811,11 @@ Verifier::~Verifier() {
|
||||
for (auto& row : *tab)
|
||||
for (auto& e : row)
|
||||
if (e) cudaGraphExecDestroy(e);
|
||||
#endif
|
||||
if (commit_exec_) cudaGraphExecDestroy(commit_exec_);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (bypass_commit_exec_) cudaGraphExecDestroy(bypass_commit_exec_);
|
||||
#endif
|
||||
for (auto& kv : exec_bm_)
|
||||
if (kv.second) cudaGraphExecDestroy(kv.second);
|
||||
for (auto& kv : commit_bm_)
|
||||
@@ -850,7 +860,11 @@ Verifier::~Verifier() {
|
||||
if (mk_counters_) cudaFree(mk_counters_);
|
||||
if (hcount_d_) cudaFree(hcount_d_);
|
||||
void* hosts[] = {h_tok_, h_step_, h_pos_, h_commit_, h_ple_, h_out_, h_x_, h_ids_, h_w_, h_seq_, h_flag_, h_ymiss_,
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
h_flagA_, h_plan_, h_flagB_, h_plan_err_, h_nf_, herr_h_, hdrop_h_, h_bal_, h_mk_stall_, h_cd_omega_};
|
||||
#else
|
||||
h_flagA_, h_plan_, h_flagB_, h_plan_err_, h_nf_, herr_h_, hdrop_h_, h_bal_, h_mk_stall_};
|
||||
#endif
|
||||
for (void* h : hosts)
|
||||
if (h) cudaFreeHost(h);
|
||||
}
|
||||
@@ -1058,6 +1072,7 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
|
||||
err = "verify: the stage's layer range or its hand-off buffers are wrong";
|
||||
return false;
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (cyclic()) {
|
||||
bool ok_c = !cyc_visits_.empty() && cyc_out_.size() == cyc_visits_.size();
|
||||
for (size_t j = 0; ok_c && j < cyc_visits_.size(); ++j) {
|
||||
@@ -1067,6 +1082,7 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
|
||||
}
|
||||
if (!ok_c) { err = "verify: the cyclic split's visits or their hand-off buffers are wrong"; return false; }
|
||||
}
|
||||
#endif
|
||||
const WeightRef* wo = wt.find("output.weight");
|
||||
if (wo == nullptr) { err = "verify: output.weight is missing"; return false; }
|
||||
n_vocab_ = wo->ne1;
|
||||
@@ -1080,8 +1096,12 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
|
||||
const uint64_t C = (uint64_t) g.ssm_conv_channels, ZV = (uint64_t) g.ssm_value_dim, HV = (uint64_t) g.ssm_v_heads;
|
||||
const uint64_t NH = (uint64_t) g.n_head, HD = (uint64_t) g.head_dim, NKV = (uint64_t) g.n_head_kv;
|
||||
const uint64_t IQ = (uint64_t) g.idx_q_heads, ID = (uint64_t) g.idx_key_dim;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const uint64_t nG = (uint64_t) (g.n_gdn_layers() + strata::kernels::loop_extra_gdn_slots(0, g.n_layers, g.qsa_interval));
|
||||
const uint64_t nQ = (uint64_t) (g.n_qsa_layers() + strata::kernels::loop_extra_qsa_slots(0, g.n_layers, g.qsa_interval));
|
||||
#else
|
||||
const uint64_t nG = (uint64_t) g.n_gdn_layers(), nQ = (uint64_t) g.n_qsa_layers();
|
||||
#endif
|
||||
const uint64_t HS = (uint64_t) strata::kernels::NG_HIST * strata::kernels::NG_HC_DIM;
|
||||
const uint64_t TS = (uint64_t) (s.idx_block - 1) * ID;
|
||||
const int max_in = (int) std::max<uint64_t>(std::max<uint64_t>(N, ZV), NH * HD);
|
||||
@@ -1105,11 +1125,13 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
|
||||
mapped(64, (void**) &h_plan_err_, (void**) &m_plan_err_) &&
|
||||
mapped(64, (void**) &h_mk_stall_, (void**) &m_mk_stall_) &&
|
||||
mapped(T * K * N * 4, (void**) &h_ymiss_, (void**) &m_ymiss_);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (ok && strata::kernels::loop_adapter().loaded()) {
|
||||
const size_t cd_cap = ((size_t) T + 3u) & ~3u; // copy_from_mapped copies multiples of 4 floats
|
||||
ok = mapped(cd_cap * 4, (void**) &h_cd_omega_, (void**) &m_cd_omega_);
|
||||
if (ok && h_cd_omega_ != nullptr) std::memset(h_cd_omega_, 0, cd_cap * 4);
|
||||
}
|
||||
#endif
|
||||
if (!ok) { err = "verify: mapped staging allocation failed " + strata::core::pin_exhausted_hint(); return false; }
|
||||
if (h_mk_stall_ != nullptr) *h_mk_stall_ = 0u;
|
||||
if (g_megakernel() && mk_counters_ == nullptr) mk_counters_ = strata::kernels::mk_counters_alloc();
|
||||
@@ -1192,17 +1214,29 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
|
||||
tap_split_ = true;
|
||||
std::fprintf(stderr, "strata: STRATA_TAP_FILE %s: %zu taps of %lld floats per row (layer split: gathered from every stage)\n", tap_split_path, nt_all, (long long) N);
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (bal_ != nullptr) { // set_pcie_balance: four stamp words per (step, group)
|
||||
const size_t bal_n = std::max<size_t>((size_t) g.n_layers,
|
||||
strata::kernels::build_loop_schedule(g.n_layers, g.qsa_interval, lb_, le_).size());
|
||||
if (!mapped(bal_n * 2 * 4 * 8, (void**) &h_bal_, (void**) &m_bal_)) {
|
||||
#else
|
||||
if (bal_ != nullptr) { // set_pcie_balance: four stamp words per (layer, group)
|
||||
if (!mapped((size_t) g.n_layers * 2 * 4 * 8, (void**) &h_bal_, (void**) &m_bal_)) {
|
||||
#endif
|
||||
err = "verify: mapped balance stamps allocation failed";
|
||||
return false;
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
bal_p_.assign(bal_n * 2, 0.0);
|
||||
bal_nc_.assign(bal_n * 2, 0);
|
||||
bal_np_.assign(bal_n * 2, 0);
|
||||
bal_ok_.assign(bal_n * 2, 0);
|
||||
#else
|
||||
bal_p_.assign((size_t) g.n_layers * 2, 0.0);
|
||||
bal_nc_.assign((size_t) g.n_layers * 2, 0);
|
||||
bal_np_.assign((size_t) g.n_layers * 2, 0);
|
||||
bal_ok_.assign((size_t) g.n_layers * 2, 0);
|
||||
#endif
|
||||
bal_->on = true;
|
||||
}
|
||||
// the GPU plan: counts(4) | start(cap+1) | dst(cap) | tok(cap) | pad | ptr(cap u64) | ptr2(cap u64) | start2(cap+1)
|
||||
@@ -1243,6 +1277,7 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
|
||||
tok_ = b.take<int32_t>(T); step_ = b.take<int32_t>(T * strata::kernels::kStepCount);
|
||||
pos_ = b.take<int32_t>(T * (NH + NKV + IQ)); commit_ = b.take<int32_t>(2 + T);
|
||||
ple_ = b.take<float>(T * N); emb_ = b.take<float>(T * N); R_ = b.take<float>(T * HC * N);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (strata::kernels::loop_adapter().loaded()) {
|
||||
R_anchor_ = b.take<float>(T * HC * N);
|
||||
R_mprev_ = b.take<float>(T * HC * N);
|
||||
@@ -1251,6 +1286,7 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
|
||||
ab_beta_ = b.take<float>(T * HV);
|
||||
cd_omega_ = b.take<float>(((uint64_t) T + 3ull) & ~3ull);
|
||||
}
|
||||
#endif
|
||||
mixed_ = b.take<float>(T * N); bo_ = b.take<float>(T * N);
|
||||
inj_ = b.take<float>(T * HC); inj2_ = b.take<float>(T * HC);
|
||||
lo_ = b.take<float>(T * (uint64_t) g.hc_lr); rs_ = b.take<float>(T * HC); xn_ = b.take<float>(T * HC * N);
|
||||
@@ -1518,6 +1554,7 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
|
||||
|
||||
const float* Verifier::final_R(int t) const { return R_ + (size_t) t * (size_t) (g_->hc * g_->n_embd); }
|
||||
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
void Verifier::cyc_select(int visit) {
|
||||
if (visit == cyc_loaded_ || visit < 0 || visit >= (int) cyc_graphs_.size()) return;
|
||||
CycGraphs& out = cyc_graphs_[(size_t) cyc_loaded_];
|
||||
@@ -1605,6 +1642,7 @@ void Verifier::set_loop_cd_in_thinking(bool in_thinking) {
|
||||
if (next_ != nullptr) next_->set_loop_cd_in_thinking(in_thinking);
|
||||
}
|
||||
|
||||
#endif
|
||||
// ================================ THE WINDOW, AS CAPTURED ================================
|
||||
//
|
||||
// Plan v0.3 P6 (split window): with `groups_ == 2` the window's tokens are cut into two groups A = [0, T/2 up) and
|
||||
@@ -1651,8 +1689,12 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
return t;
|
||||
};
|
||||
// fork 34a35db: the GDN gates and the QSA indexer beside the projections (STRATA_SIDE_MIXER=1; 0 / unset = in line)
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool loop_on = loop_adapter().loaded() && !bypass_loaded_;
|
||||
const int side_mixer_mode = loop_on ? 0 : g_side_mixer();
|
||||
#else
|
||||
const int side_mixer_mode = g_side_mixer();
|
||||
#endif
|
||||
const bool side_mixer = side_mixer_mode >= 1;
|
||||
const bool batch_side = batch_rec_ && batch_side_mixer_on();
|
||||
const bool side_proj = side_mixer_mode >= 2 && (!batch_rec_ || batch_side); // fork b063c13: the GDN gate projection, the QSA queries
|
||||
@@ -1777,8 +1819,10 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
// ---- the window's inputs, from mapped staging. With branches the steps and positions (first read by the first
|
||||
// layer's mixer) cross PCIe on side 1 beside the embedding and the first hyper-connection read.
|
||||
copy_i32_from_mapped(tok_, m_tok_, T, cs);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on && cd_omega_ != nullptr && m_cd_omega_ != nullptr)
|
||||
copy_from_mapped(cd_omega_, m_cd_omega_, ((int64_t) T + 3) & ~3ll, cs);
|
||||
#endif
|
||||
bool inputs_pending = false;
|
||||
cudaStream_t in_s = cs;
|
||||
if (br) {
|
||||
@@ -1795,21 +1839,32 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
// ---- the embeddings, broadcast to the hc streams - or, in a later stage of a layer split, the previous stage's
|
||||
// residual, pending write and inject (see set_stage)
|
||||
const int64_t HB = Verifier::handoff_floats(g);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool cyc = cyclic() && !bypass_loaded_ && !batch_rec_;
|
||||
const strata::kernels::LoopVisit* cvis = cyc ? &cyc_visits_[(size_t) cyc_visit_] : nullptr;
|
||||
if (cyc ? !cvis->embed : lb_ > 0) {
|
||||
const float* hin = (cyc ? cyc_hand_in_ : hand_in_) + (size_t) hrow0 * HB; // the group's rows: [R][bo][inj] contiguous
|
||||
#else
|
||||
if (lb_ > 0) {
|
||||
const float* hin = hand_in_ + (size_t) hrow0 * HB; // the group's rows: [R][bo][inj] contiguous
|
||||
#endif
|
||||
// the overlapped split: this graph was launched before the previous stage finished - wait for its hand-off
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (hflag_in_d_ != nullptr && !batch_rec_ && !cyc) wait_handoff(hflag_in_d_, 1u, herr_d_, g_split_wait_ns, cs);
|
||||
#else
|
||||
if (hflag_in_d_ != nullptr && !batch_rec_) wait_handoff(hflag_in_d_, 1u, herr_d_, g_split_wait_ns, cs);
|
||||
#endif
|
||||
copy_from_mapped(R_, hin, (int64_t) T * HC * N, cs);
|
||||
copy_from_mapped(bo_, hin + (size_t) T * HC * N, (int64_t) T * N, cs);
|
||||
copy_from_mapped(inj2_, hin + (size_t) T * (HC + 1) * N, (int64_t) T * HC, cs);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// a cyclic visit on a later pass: the previous pass's core output - the residual itself on the wrap into the
|
||||
// core's first stage, else relayed after the hand-off (the workspace entry overwrites R, so never an alias)
|
||||
if (cyc && cvis->mprev_in == 1)
|
||||
cudaMemcpyAsync(R_mprev_, R_, (size_t) T * HC * N * sizeof(float), cudaMemcpyDeviceToDevice, cs);
|
||||
else if (cyc && cvis->mprev_in == 2)
|
||||
copy_from_mapped(R_mprev_, hin + (size_t) T * HB, (int64_t) T * HC * N, cs);
|
||||
#endif
|
||||
} else if (const NativeEmbed* ne = native_embed()) { // plan v0.3 P6: the GGUF-form table
|
||||
ne->gather_dev(tok_, T, emb_, cs);
|
||||
broadcast_streams(emb_, R_, N, (int) HC, T, cs);
|
||||
@@ -1829,16 +1884,34 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
broadcast_streams(emb_, R_, N, (int) HC, T, cs);
|
||||
}
|
||||
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// execution schedule (0..n_layers-1 when loop adapter is off or bypassed; expands core passes when on)
|
||||
const auto loop_plan = cyc ? cvis->steps : build_loop_schedule(g.n_layers, g.qsa_interval, lb_, le_, bypass_loaded_);
|
||||
#else
|
||||
// per-layer state indices (GDN and QSA layers are numbered separately)
|
||||
std::vector<int64_t> gdn_idx((size_t) g.n_layers, -1), qsa_idx((size_t) g.n_layers, -1);
|
||||
{
|
||||
int64_t qi = 0, gi = 0;
|
||||
for (int64_t l = 0; l < g.n_layers; ++l) {
|
||||
if (is_qsa_layer(g, l)) qsa_idx[(size_t) l] = qi++;
|
||||
else gdn_idx[(size_t) l] = gi++;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// ---------------------------------------------------------------- pre(sidx, group): up to the ring
|
||||
auto pre = [&](size_t sidx, int grp) -> bool {
|
||||
const LoopStep& stp = loop_plan[sidx];
|
||||
const int64_t l = stp.layer;
|
||||
const int pass = stp.pass;
|
||||
#else
|
||||
// ---------------------------------------------------------------- pre(l, group): up to the ring
|
||||
auto pre = [&](int64_t l, int grp) -> bool {
|
||||
#endif
|
||||
const int tb = tb_[grp], te = te_[grp], n = te - tb;
|
||||
stamp(l, 0, grp);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (stp.save_anchor_before) {
|
||||
cudaMemcpyAsync(R_anchor_ + (size_t) tb * HC * N, Rt(tb), (size_t) n * HC * N * sizeof(float),
|
||||
cudaMemcpyDeviceToDevice, cs);
|
||||
@@ -1850,6 +1923,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
if (stp.run_cross_pass_mixer) {
|
||||
loop_cross_pass_mixer(Rt(tb), R_mprev_ + (size_t) tb * HC * N, Rt(tb), pass - 1, stp.core_step, n, cs);
|
||||
}
|
||||
#endif
|
||||
const LayerView v(wt, l);
|
||||
const char* pfx[2] = {"hc_attn_", "hc_ffn_"};
|
||||
const WeightRef *wn[2], *wd[2], *wu[2], *wi[2];
|
||||
@@ -1860,11 +1934,18 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
wi[h] = need(v, (std::string(pfx[h]) + "inject.weight").c_str(), err);
|
||||
if (!wn[h] || !wd[h] || !wu[h] || !wi[h]) return false;
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// the previous step's FFN write, folded into this step's first read (unless already materialized)
|
||||
bool pending = loop_on
|
||||
? (stp.pending_before && sidx > 0 && !cvec().covers(loop_plan[sidx - 1].layer))
|
||||
: (l > 0 && !cvec().covers(l - 1));
|
||||
if (l == 1 && pass == 0 && ple_on) {
|
||||
#else
|
||||
// the previous layer's FFN write, folded into this layer's first read (a control vector after it has
|
||||
// already applied it)
|
||||
bool pending = l > 0 && !cvec().covers(l - 1);
|
||||
if (l == 1 && ple_on) {
|
||||
#endif
|
||||
if (grp == 0) {
|
||||
if (ar_on()) wait_flag_ge(m_flag_, 1, cs);
|
||||
copy_from_mapped(ple_, m_ple_, (int64_t) T * N, cs);
|
||||
@@ -2037,13 +2118,18 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
if (!native_of(wqkv, v.name("attn_qkv.weight"), err) || !native_of(wg, v.name("attn_gate.weight"), err) ||
|
||||
!native_of(wout, v.name("ssm_out.weight"), err))
|
||||
return false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const int64_t gi = stp.gdn_slot;
|
||||
#else
|
||||
const int64_t gi = gdn_idx[(size_t) l];
|
||||
#endif
|
||||
float* state = ss.gdn_state + (size_t) (gi - ss.gdn_ord0) * gdn_floats;
|
||||
float* conv = state + (uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size;
|
||||
float* qkv = qkv_L_ + (size_t) gi * MT * C;
|
||||
float* hb = h_L_ + (size_t) gi * MT * C;
|
||||
float* gate = gate_L_ + (size_t) gi * MT * HV;
|
||||
float* beta = beta_L_ + (size_t) gi * MT * HV;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool has_gdn_l = loop_on && loop_adapter().has_gdn_lora(pass, l);
|
||||
const bool has_ab_l = loop_on && (loop_adapter().has_lora(pass, l, LoopTarget::kGdnA) ||
|
||||
loop_adapter().has_lora(pass, l, LoopTarget::kGdnB));
|
||||
@@ -2052,6 +2138,10 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
const bool gdn_br = br && !has_gdn_l;
|
||||
const bool use_mk = !has_gdn_l && g_megakernel() && mk_counters_ != nullptr && lb_ == 0 && le_ == g.n_layers &&
|
||||
next_ == nullptr && !gdn_br && !gdn_sidem && !gdn_side_proj && !batch_rec_ && !self_commit &&
|
||||
#else
|
||||
const bool use_mk = g_megakernel() && mk_counters_ != nullptr && lb_ == 0 && le_ == g.n_layers &&
|
||||
next_ == nullptr && !br && !sidem && !side_proj && !batch_rec_ && !self_commit &&
|
||||
#endif
|
||||
tb == 0 && n >= 1 && n <= 8 && wqkv->native_type == 8 && wg->native_type == 8 &&
|
||||
wout->native_type == 8;
|
||||
if (use_mk) {
|
||||
@@ -2069,7 +2159,11 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
stamp(l, 2, grp); stamp(l, 3, grp); stamp(l, 4, grp); stamp(l, 5, grp); stamp(l, 6, grp);
|
||||
il_ready = false;
|
||||
} else {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (gdn_sidem) { // the gates (a few blocks, latency-bound) beside the projections, joined below
|
||||
#else
|
||||
if (sidem) { // the gates (a few blocks, latency-bound) beside the projections, joined below
|
||||
#endif
|
||||
if (!branch(mfork_, cs, side_, "fork")) return false;
|
||||
gdn_ab_multi(xm, (const uint16_t*) wa->data, (const uint16_t*) wb->data, (const float*) wdt->data,
|
||||
(const float*) wsa->data, gate + (size_t) tb * HV, beta + (size_t) tb * HV, (int) N,
|
||||
@@ -2078,7 +2172,11 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
if (cudaEventRecord(mjoin_, side_) != cudaSuccess) { err = "verify: the GDN gates' branch could not join"; return false; }
|
||||
}
|
||||
if (!q8_attn) quant_in(xm, (int) N);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (gdn_side_proj) { // the gate projection (compute-bound for i-quants) beside qkv and the conv
|
||||
#else
|
||||
if (side_proj) { // the gate projection (compute-bound for i-quants) beside qkv and the conv
|
||||
#endif
|
||||
// the branch reads F4's interleaved copy too (written on cs before the fork; bitwise native_mmvq's
|
||||
// output), and qkv is recorded before the branch's launch, so its grid is dispatched first and the
|
||||
// conv / recurrence that wait for it start sooner (z is read only after the recurrence)
|
||||
@@ -2094,7 +2192,11 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
else native_mmvq(wg->native_type, wg->native_data, xq_, z_ + (size_t) tb * ZV, (int) N, (int) ZV, n, side2_);
|
||||
if (cudaEventRecord(pjoin_, side2_) != cudaSuccess) { err = "verify: the gate projection's branch could not join"; return false; }
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (gdn_br) { // STRATA_DF_BRANCH: a/b and z beside q/k/v and the conv; all of them read only this layer's input
|
||||
#else
|
||||
if (br) { // STRATA_DF_BRANCH: a/b and z beside q/k/v and the conv; all of them read only this layer's input
|
||||
#endif
|
||||
fork(0);
|
||||
gdn_ab_multi(xm, (const uint16_t*) wa->data, (const uint16_t*) wb->data, (const float*) wdt->data,
|
||||
(const float*) wsa->data, gate + (size_t) tb * HV, beta + (size_t) tb * HV, (int) N,
|
||||
@@ -2103,11 +2205,20 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
native_mmvq(wg->native_type, wg->native_data, xq_, z_ + (size_t) tb * ZV, (int) N, (int) ZV, n,
|
||||
df_side_[1]);
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool fuse_ab = fuse_convab_on() && !gdn_br && !batch_rec_ && !prof_on_ && !gdn_sidem && !has_ab_l;
|
||||
strata::dispatch::notef("gdn layer: side_gates=%ld side_proj=%ld df_branch=%ld", (long) gdn_sidem, (long) gdn_side_proj, (long) gdn_br);
|
||||
#else
|
||||
const bool fuse_ab = fuse_convab_on() && !br && !batch_rec_ && !prof_on_ && !sidem; // the side mixer computes the gates on its branch
|
||||
strata::dispatch::notef("gdn layer: side_gates=%ld side_proj=%ld df_branch=%ld", (long) sidem, (long) side_proj, (long) br);
|
||||
#endif
|
||||
strata::dispatch::notef("gdn layer: conv+ab fused=%ld batch_rows=%ld", (long) fuse_ab, (long) batch_rec_);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (!gdn_side_proj) mm(wqkv, qkv + (size_t) tb * C, (int) N, (int) C); // with side_proj: above, first
|
||||
if (has_gdn_l) loop_lora_add(xm, (int) N, qkv + (size_t) tb * C, (int) C, pass, l, LoopTarget::kGdnQkv, n, cs);
|
||||
#else
|
||||
if (!side_proj) mm(wqkv, qkv + (size_t) tb * C, (int) N, (int) C); // with side_proj: above, first
|
||||
#endif
|
||||
stamp(l, 2, grp);
|
||||
if (batch_rec_) { // contiguous rows may be proposals for the same slot
|
||||
for (int t = tb; t < te;) {
|
||||
@@ -2131,10 +2242,15 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
gdn_conv_l2_multi(conv, qkv, (const float*) wc->data, hb, (int) C, (int) (2 * HK), EPS, n, cs, tb,
|
||||
self_commit);
|
||||
stamp(l, 3, grp);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (gdn_br) { // gate, beta and z, before the recurrence reads them
|
||||
#else
|
||||
if (br) { // gate, beta and z, before the recurrence reads them
|
||||
#endif
|
||||
join(0);
|
||||
join(1);
|
||||
} else {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (has_ab_l) {
|
||||
bf16_gemv_fp32_mmvf_multi(xm, N, (const uint16_t*) wa->data, ab_alpha_ + (size_t) tb * HV, HV, N, HV, n, cs);
|
||||
bf16_gemv_fp32_mmvf_multi(xm, N, (const uint16_t*) wb->data, ab_beta_ + (size_t) tb * HV, HV, N, HV, n, cs);
|
||||
@@ -2144,18 +2260,33 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
(const float*) wdt->data, (const float*) wsa->data,
|
||||
gate + (size_t) tb * HV, beta + (size_t) tb * HV, (int) HV, n, cs);
|
||||
} else if (!fuse_ab && !gdn_sidem) // the gates: the HIP fused conv kernel (143-amdk) or the side mixer's branch, else here
|
||||
#else
|
||||
if (!fuse_ab && !sidem) // the gates: the HIP fused conv kernel (143-amdk) or the side mixer's branch, else here
|
||||
#endif
|
||||
gdn_ab_multi(xm, (const uint16_t*) wa->data, (const uint16_t*) wb->data, (const float*) wdt->data,
|
||||
(const float*) wsa->data, gate + (size_t) tb * HV, beta + (size_t) tb * HV, (int) N, (int) HV,
|
||||
n, cs);
|
||||
stamp(l, 4, grp);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (!gdn_side_proj) mm(wg, z_ + (size_t) tb * ZV, (int) N, (int) ZV);
|
||||
if (has_gdn_l) loop_lora_add(xm, (int) N, z_ + (size_t) tb * ZV, (int) ZV, pass, l, LoopTarget::kGdnZ, n, cs);
|
||||
#else
|
||||
if (!side_proj) mm(wg, z_ + (size_t) tb * ZV, (int) N, (int) ZV);
|
||||
#endif
|
||||
stamp(l, 5, grp);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (gdn_side_proj && cudaStreamWaitEvent(cs, pjoin_, 0) != cudaSuccess) {
|
||||
#else
|
||||
if (side_proj && cudaStreamWaitEvent(cs, pjoin_, 0) != cudaSuccess) {
|
||||
#endif
|
||||
err = "verify: the gate projection's branch could not join";
|
||||
return false;
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (gdn_sidem && cudaStreamWaitEvent(cs, mjoin_, 0) != cudaSuccess) {
|
||||
#else
|
||||
if (sidem && cudaStreamWaitEvent(cs, mjoin_, 0) != cudaSuccess) {
|
||||
#endif
|
||||
err = "verify: the GDN gates' branch could not join";
|
||||
return false;
|
||||
}
|
||||
@@ -2196,16 +2327,24 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
if (!g_qfuse() || batch_rec_) quant_in(y_ + (size_t) tb * ZV, (int) ZV);
|
||||
else il_ready = false;
|
||||
mm(wout, bo_ + tb * N, (int) ZV, (int) N);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (has_gdn_l) loop_lora_add(y_ + (size_t) tb * ZV, (int) ZV, bo_ + (size_t) tb * N, (int) N, pass, l, LoopTarget::kGdnOut, n, cs);
|
||||
#endif
|
||||
}
|
||||
} else {
|
||||
// ======================= QSA =======================
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const int64_t qi = stp.qsa_slot;
|
||||
#else
|
||||
const int64_t qi = qsa_idx[(size_t) l];
|
||||
#endif
|
||||
const QsaState& st = ss.qsa_states[qi];
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool has_qsa_l = loop_on && (loop_adapter().has_lora(pass, l, LoopTarget::kQsaQ) ||
|
||||
loop_adapter().has_lora(pass, l, LoopTarget::kQsaK) ||
|
||||
loop_adapter().has_lora(pass, l, LoopTarget::kQsaV) ||
|
||||
loop_adapter().has_lora(pass, l, LoopTarget::kQsaO));
|
||||
#endif
|
||||
const WeightRef *wik = need(v, "indexer.k_proj.weight", err), *wq = need(v, "attn_q.weight", err),
|
||||
*wk = need(v, "attn_k.weight", err), *wv = need(v, "attn_v.weight", err),
|
||||
*wo = need(v, "attn_output.weight", err), *wiq = need(v, "indexer.q_proj.weight", err),
|
||||
@@ -2250,7 +2389,11 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
if (!q8_attn) quant_in(xm, (int) N);
|
||||
// STRATA_DF_BRANCH: the query side (q and its norm/RoPE/rotation on side 0, the indexer query on side 1)
|
||||
// beside the K/V side; the indexer query joins before the block scores, the query before attention
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool qbr = br && qb && !has_qsa_l;
|
||||
#else
|
||||
const bool qbr = br && qb;
|
||||
#endif
|
||||
if (qbr) {
|
||||
fork(0);
|
||||
native_mmvq(wq->native_type, wq->native_data, xq_, qfull_ + tb * NH * 2 * HD, (int) N,
|
||||
@@ -2476,9 +2619,11 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
qfull_ + tb * NH * 2 * HD, (int) N, (int) (NH * 2 * HD), n, ns);
|
||||
else native_mmvq(wq->native_type, wq->native_data, xq_, qfull_ + tb * NH * 2 * HD, (int) N,
|
||||
(int) (NH * 2 * HD), n, ns);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (has_qsa_l)
|
||||
loop_lora_add(xm, (int) N, qfull_ + (size_t) tb * NH * 2 * HD, (int) (NH * 2 * HD), pass, l,
|
||||
LoopTarget::kQsaQ, n, ns);
|
||||
#endif
|
||||
if (qb) {
|
||||
if (fuse_nr) { // the q/gate split reads q straight out of the q|gate rows (stride 2 * HD)
|
||||
native_qsa_rms_norm_rope(qfull_ + tb * NH * 2 * HD, (int) (2 * HD), (const float*) wqn->data,
|
||||
@@ -2530,12 +2675,14 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
stamp(l, 7, grp);
|
||||
mm(wk, kcur_ + tb * NKV * HD, (int) N, (int) (NKV * HD));
|
||||
mm(wv, vcur_ + tb * NKV * HD, (int) N, (int) (NKV * HD));
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (has_qsa_l) {
|
||||
loop_lora_add(xm, (int) N, kcur_ + (size_t) tb * NKV * HD, (int) (NKV * HD), pass, l,
|
||||
LoopTarget::kQsaK, n, cs);
|
||||
loop_lora_add(xm, (int) N, vcur_ + (size_t) tb * NKV * HD, (int) (NKV * HD), pass, l,
|
||||
LoopTarget::kQsaV, n, cs);
|
||||
}
|
||||
#endif
|
||||
if (qb) norm_rope(kcur_ + tb * NKV * HD, wkn, (int) (n * NKV), (int) HD, pos_k + tb * NKV);
|
||||
else for (int t = tb; t < te; ++t) norm_rope(kcur_ + t * NKV * HD, wkn, (int) NKV, (int) HD, pos_ + t * NH);
|
||||
if (st.kv_rot) { // K and V rotated before they are stored (kv_q4.hpp)
|
||||
@@ -2803,17 +2950,21 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
stamp(l, 14, grp);
|
||||
quant_in(attn32_ + tb * NH * HD, (int) (NH * HD));
|
||||
mm(wo, bo_ + tb * N, (int) (NH * HD), (int) N);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (has_qsa_l)
|
||||
loop_lora_add(attn32_ + (size_t) tb * NH * HD, (int) (NH * HD), bo_ + (size_t) tb * N, (int) N,
|
||||
pass, l, LoopTarget::kQsaO, n, cs);
|
||||
#endif
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
err = "verify layer " + std::to_string(l) + ": " + e.what();
|
||||
return false;
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on && loop_adapter().dual_coda) // strata_dual_coda_loop_v1: attn LoRA on the block's HC-mixed input, added to its output
|
||||
loop_lora_add_gated(mixed_ + (size_t) tb * N, (int) N, bo_ + (size_t) tb * N, (int) N, pass, l,
|
||||
LoopTarget::kAttn, n, cs, cd_omega_ + tb);
|
||||
#endif
|
||||
stamp(l, 16, grp);
|
||||
const bool q8_ffn = gr_read_group(1, true, inj_, inj2_); // true: nat_xq_ holds the MoE input's q8_1 (STRATA_QFUSE)
|
||||
stamp(l, 32, grp); // the profile only: the FFN-side read ends here, the router starts
|
||||
@@ -2834,11 +2985,18 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
// the window's rows routed in 2 launches (one router GEMV reading the weight once, one
|
||||
// top-10) instead of 2 per token; every row's arithmetic is the single-token call's (STRATA_DEC_BATCH=0: old)
|
||||
const WeightRef* w_router = v.get("ffn_gate_inp.weight");
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool has_gate_l = loop_on && loop_adapter().has_lora(pass, l, LoopTarget::kMoeGate);
|
||||
#endif
|
||||
// S26 STRATA_LFUSE=1: the shared expert's gate row rides in the router GEMV launch, its sigmoid + row scale
|
||||
// move into the combine, gate + up share one launch (all bitwise; resident combine path, 2-8 rows only)
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const WeightRef* w_sgi = (!has_gate_l && lfuse_on(n) && g_lfuse_gate()) ? v.get("ffn_gate_inp_shexp.weight") : nullptr;
|
||||
#else
|
||||
const WeightRef* w_sgi = lfuse_on(n) && g_lfuse_gate() ? v.get("ffn_gate_inp_shexp.weight") : nullptr;
|
||||
#endif
|
||||
bool sg_ready = false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (has_gate_l && w_router != nullptr) {
|
||||
try {
|
||||
bf16_gemv_fp32_mmvf_multi(mixed_ + tb * N, N, (const uint16_t*) w_router->data, logits_ + tb * NE, NE, N,
|
||||
@@ -2851,6 +3009,9 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
router_top10(logits_ + tb * NE, n, (int) NE, (int) K, ids_ + tb * K, w_ + tb * K, cs);
|
||||
} catch (const std::exception& e) { err = "verify router (loop): " + std::string(e.what()); return false; }
|
||||
} else if (dec_batch && n > 1 && w_router != nullptr && native_router_enabled() && NE == 512 && K == 10) {
|
||||
#else
|
||||
if (dec_batch && n > 1 && w_router != nullptr && native_router_enabled() && NE == 512 && K == 10) {
|
||||
#endif
|
||||
try {
|
||||
if (w_sgi != nullptr)
|
||||
sg_ready = bf16_gemv_fp32_mmvf_multi_aux(mixed_ + tb * N, N, (const uint16_t*) w_router->data,
|
||||
@@ -2913,11 +3074,19 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
resident_plan(ids_ + tb * K, n * (int) K, (int) K, hits_.d_res + l * g.n_expert, (int) g.n_expert,
|
||||
hits_.cache_base, slot_off_d_, (long long) hits_.blob,
|
||||
plan_ + (size_t) grp * (size_t) (plan_i32_ + 16), (long long) max_t_ * K, skip_ + grp,
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
(uint32_t) ((int64_t) sidx * G + grp + 1), cs, nullptr, peer_autonomous_);
|
||||
#else
|
||||
(uint32_t) ((l - lb_) * G + grp + 1), cs, nullptr, peer_autonomous_);
|
||||
#endif
|
||||
#if defined(STRATA_USE_HIP)
|
||||
if (g_doorbell_store) // #649 A/B: the step's ring stored, not incremented over PCIe
|
||||
doorbell_publish_value(xm, ids_ + tb * K, w_ + tb * K, (int64_t) n * N, (int64_t) n * K, m_x_ + tb * N,
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
m_ids_ + tb * K, m_w_ + tb * K, m_seq_, (uint32_t) ((int64_t) sidx * G + grp + 1), cs);
|
||||
#else
|
||||
m_ids_ + tb * K, m_w_ + tb * K, m_seq_, (uint32_t) ((l - lb_) * G + grp + 1), cs);
|
||||
#endif
|
||||
else
|
||||
#endif
|
||||
// #578: the helper GPUs reduce with the routing weights - publish them; set_always_publish: the rows
|
||||
@@ -2964,8 +3133,12 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
shared_expert_multi(n, xm, sh_bf16_ + tb * N, nsw, (const uint16_t*) wgi->data, sh_gate_ + (size_t) tb * g.n_ff,
|
||||
sh_up_ + (size_t) tb * g.n_ff, sh_g_ + tb, shared_ + tb * N, N, g.n_ff, sh_stream,
|
||||
qdedup ? (const void*) (nat_xq_ + (size_t) tb * (N / 32) * 36) : nullptr,
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
(sg_ready ? 1 : 0) | (lfuse_on(n) && g_lfuse_pair() ? 2 : 0),
|
||||
loop_on ? pass : -1, loop_on ? l : -1);
|
||||
#else
|
||||
(sg_ready ? 1 : 0) | (lfuse_on(n) && g_lfuse_pair() ? 2 : 0));
|
||||
#endif
|
||||
sg_gated_[grp] = sg_ready;
|
||||
} catch (const std::exception& e) {
|
||||
err = std::string("verify shared expert: ") + e.what();
|
||||
@@ -2999,14 +3172,26 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
const char* e = std::getenv("STRATA_FUSE_HEAD_GR");
|
||||
return e && e[0] == '1';
|
||||
}();
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool fuse_head_gr = !loop_on && fuse_head_gr_env && (le_ == g.n_layers) && (head_ != nullptr && head_->loaded()) &&
|
||||
#else
|
||||
const bool fuse_head_gr = fuse_head_gr_env && (le_ == g.n_layers) && (head_ != nullptr && head_->loaded()) &&
|
||||
#endif
|
||||
!cvec().covers(g.n_layers - 1);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
auto post = [&](size_t sidx, int grp) -> bool {
|
||||
const LoopStep& stp = loop_plan[sidx];
|
||||
const int64_t l = stp.layer;
|
||||
const int pass = stp.pass;
|
||||
#else
|
||||
auto post = [&](int64_t l, int grp) -> bool {
|
||||
#endif
|
||||
const int tb = tb_[grp], te = te_[grp], n = te - tb;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const uint32_t ring = (uint32_t) ((int64_t) sidx * G + grp + 1);
|
||||
#else
|
||||
const uint32_t ring = (uint32_t) ((l - lb_) * G + grp + 1);
|
||||
#endif
|
||||
const int64_t cap = (int64_t) n * K, capx = (int64_t) max_t_ * K;
|
||||
int32_t* pl = plan_ + (size_t) grp * (size_t) (plan_i32_ + 16);
|
||||
const int32_t* p_counts = pl;
|
||||
@@ -3090,7 +3275,11 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
wait_copy_i32_from_mapped(m_flagA_, ring, pl, m_plan_ + (size_t) grp * (size_t) plan_i32_, plan_i32_, cs);
|
||||
} else {
|
||||
if (m_bal_ != nullptr) // set_pcie_balance: the GPU's share starts as the plan arrives
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
wait_flag_ge_stamped(m_flagA_, ring, nullptr, m_bal_ + ((int64_t) sidx * 2 + grp) * 4, cs);
|
||||
#else
|
||||
wait_flag_ge_stamped(m_flagA_, ring, nullptr, m_bal_ + ((l - lb_) * 2 + grp) * 4, cs);
|
||||
#endif
|
||||
else
|
||||
wait_flag_ge(m_flagA_, ring, cs); // the pool published this group's GPU plan
|
||||
copy_i32_from_mapped(pl, m_plan_ + (size_t) grp * (size_t) plan_i32_, plan_i32_, cs);
|
||||
@@ -3101,7 +3290,11 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
stamp(l, 20, grp);
|
||||
if (device_plan_) wait_flag_ge_or(m_flagB_, ring, skip_ + grp, cs);
|
||||
else if (m_bal_ != nullptr) // set_pcie_balance: the PCIe part starts
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
wait_flag_ge_stamped(m_flagB_, ring, m_bal_ + ((int64_t) sidx * 2 + grp) * 4 + 1, nullptr, cs);
|
||||
#else
|
||||
wait_flag_ge_stamped(m_flagB_, ring, m_bal_ + ((l - lb_) * 2 + grp) * 4 + 1, nullptr, cs);
|
||||
#endif
|
||||
else wait_flag_ge(m_flagB_, ring, cs); // the PCIe share is in staging (DMA) or mapped
|
||||
if (sink_.pcie_mode == 2) { // stage it with a copy kernel, then point at staging
|
||||
const int64_t per = G == 2 ? kStagingBlobs / 2 : kStagingBlobs;
|
||||
@@ -3134,7 +3327,11 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
const bool fuse_rows = fuse_rows_on() && !prof_on_ && !remote_opt_ && dec_batch && m_bal_ == nullptr; // 143-amdk
|
||||
if (!fuse_rows) {
|
||||
if (m_bal_ != nullptr) // set_pcie_balance: ... and ends as it waits for the CPU's rows
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
wait_flag_ge_stamped(m_flag_, ring, m_bal_ + ((int64_t) sidx * 2 + grp) * 4 + 2, nullptr, cs);
|
||||
#else
|
||||
wait_flag_ge_stamped(m_flag_, ring, m_bal_ + ((l - lb_) * 2 + grp) * 4 + 2, nullptr, cs);
|
||||
#endif
|
||||
else
|
||||
wait_flag_ge(m_flag_, ring, cs); // the CPU's share is in the mapped rows
|
||||
}
|
||||
@@ -3186,10 +3383,13 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
}
|
||||
if (remote_opt_) remote_opt_->combine(bo_ + tb * N, y_dummy_ + tb * N, tb, n,
|
||||
device_plan_ ? skip_ + grp : nullptr, ring, cs);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on && loop_adapter().dual_coda) // strata_dual_coda_loop_v1: mlp LoRA on the MoE's HC-mixed input, added to its output
|
||||
loop_lora_add_gated(mixed_ + (size_t) tb * N, (int) N, bo_ + (size_t) tb * N, (int) N, pass, l,
|
||||
LoopTarget::kMlp, n, cs, cd_omega_ + tb);
|
||||
#endif
|
||||
stamp(l, 24, grp);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// LoopCD-Hidden on this step's rows (the core's last layer on the last pass): a tap here takes the corrected rows
|
||||
// after it, as the prompt path does. Always captured when loop_on so per-token thinking gating and per-request
|
||||
// loop_cd_omega changes work without recapturing CUDA graphs (omega == 0.0f is an immediate bit-exact no-op).
|
||||
@@ -3225,6 +3425,9 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
if (cd_here && tap_here) tap_materialized();
|
||||
};
|
||||
if (tap_here && !cd_here) {
|
||||
#else
|
||||
if (tap_on_ && (!batch_rec_ || tap_batch_) && l < (int64_t) tap_slot_.size() && tap_slot_[(size_t) l] >= 0) {
|
||||
#endif
|
||||
// the layer's completed output: R with this FFN half's write applied (the next layer's read would fold it), the mean of
|
||||
// the hc streams, all rows of the group in one launch
|
||||
const int64_t nt = (int64_t) tap_layers_.size();
|
||||
@@ -3239,20 +3442,28 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// the model's final step: the head reads the materialized rows (a core ending at the last layer runs its
|
||||
// checkpoints and LoopCD on them first)
|
||||
if (cyc ? (cvis->head && sidx + 1 == loop_plan.size()) : (l == g.n_layers - 1 && sidx + 1 == loop_plan.size())) {
|
||||
#else
|
||||
if (l == g.n_layers - 1) {
|
||||
#endif
|
||||
if (!fuse_head_gr) {
|
||||
if (dec_batch && !g_no_multi_gr) gr_write_multi(Rt(tb), bo_ + tb * N, inj2_ + tb * HC, gs, Rt(tb), n, cs);
|
||||
else for (int t = tb; t < te; ++t) gr_write(Rt(t), bo_ + t * N, inj2_ + t * HC, gs, Rt(t), cs);
|
||||
if (cvec().covers(l)) cvec_apply(Rt(tb), l, n, HC * N, nullptr, 0, nullptr, 0, false, cs);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on && stp.materialize_after) loop_after();
|
||||
#endif
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
} else if (loop_on && stp.materialize_after) {
|
||||
if (dec_batch && !g_no_multi_gr) gr_write_multi(Rt(tb), bo_ + tb * N, inj2_ + tb * HC, gs, Rt(tb), n, cs);
|
||||
else for (int t = tb; t < te; ++t) gr_write(Rt(t), bo_ + t * N, inj2_ + t * HC, gs, Rt(t), cs);
|
||||
if (cvec().covers(l)) cvec_apply(Rt(tb), l, n, HC * N, nullptr, 0, nullptr, 0, false, cs);
|
||||
loop_after();
|
||||
#endif
|
||||
} else if (cvec().covers(l)) {
|
||||
cvec_apply(Rt(tb), l, n, HC * N, bo_ + tb * N, N, inj2_ + tb * HC, HC, true, cs);
|
||||
}
|
||||
@@ -3260,12 +3471,23 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
};
|
||||
|
||||
for (int grp = 0; grp < G; ++grp)
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (!pre(0, grp)) return false;
|
||||
for (size_t sidx = 0; sidx < loop_plan.size(); ++sidx)
|
||||
#else
|
||||
if (!pre(lb_, grp)) return false;
|
||||
for (int64_t l = lb_; l < le_; ++l)
|
||||
#endif
|
||||
for (int grp = 0; grp < G; ++grp) {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (!post(sidx, grp)) return false;
|
||||
if (sidx + 1 < loop_plan.size() && !pre(sidx + 1, grp)) return false;
|
||||
#else
|
||||
if (!post(l, grp)) return false;
|
||||
if (l + 1 < le_ && !pre(l + 1, grp)) return false;
|
||||
#endif
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (cyc && !cvis->head) { // a cyclic visit before the last: the materialized residual (and m_prev) on
|
||||
float* hout = cyc_out_[(size_t) cyc_visit_];
|
||||
copy_from_mapped(hout, R_, (int64_t) T * HC * N, cs);
|
||||
@@ -3275,6 +3497,9 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
return true;
|
||||
}
|
||||
if (!cyc && le_ < g.n_layers) { // a layer split's earlier stage: hand the residual on, no head
|
||||
#else
|
||||
if (le_ < g.n_layers) { // a layer split's earlier stage: hand the residual on, no head
|
||||
#endif
|
||||
float* hout = hand_out_ + (size_t) hrow0 * HB;
|
||||
if (hflag_out_d_ != nullptr && !batch_rec_) { // the overlapped split: payload + the next stage's flag
|
||||
handoff_publish(hout, R_, (int64_t) T * HC * N, bo_, (int64_t) T * N, inj2_, (int64_t) T * HC, hcount_d_,
|
||||
@@ -3724,10 +3949,16 @@ bool Verifier::capture_commit(std::string& err) {
|
||||
#else
|
||||
static const bool commit_batch = [] { const char* e = std::getenv("STRATA_COMMIT_BATCH"); return !e || e[0] != '0'; }();
|
||||
#endif
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool commit_loop_on = loop_adapter().loaded() && !bypass_loaded_;
|
||||
const bool commit_batch_eff = commit_batch && !commit_loop_on;
|
||||
#endif
|
||||
bool gdn_batched = false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (commit_batch_eff) {
|
||||
#else
|
||||
if (commit_batch) {
|
||||
#endif
|
||||
int64_t n_gdn = 0;
|
||||
bool norms_ok = true; // the per-layer loop checks each layer's ssm_norm.weight: keep that check
|
||||
for (int64_t l = lb_; l < le_ && norms_ok; ++l)
|
||||
@@ -3746,7 +3977,11 @@ bool Verifier::capture_commit(std::string& err) {
|
||||
}
|
||||
// the QSA layers' tail restore + indexer append, one launch for all of them (one block per layer)
|
||||
bool qsa_batched = false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (commit_batch_eff && ok && !g_no_batch_kv) {
|
||||
#else
|
||||
if (commit_batch && ok && !g_no_batch_kv) {
|
||||
#endif
|
||||
QsaIndexerLayers il;
|
||||
int nq = 0;
|
||||
int64_t mc = -1;
|
||||
@@ -3771,19 +4006,25 @@ bool Verifier::capture_commit(std::string& err) {
|
||||
qsa_batched = true;
|
||||
}
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const auto commit_plan = (cyclic() && !bypass_loaded_)
|
||||
? cyc_owner_steps()
|
||||
: build_loop_schedule(g.n_layers, g.qsa_interval, lb_, le_, bypass_loaded_);
|
||||
for (size_t sidx = 0; sidx < commit_plan.size() && ok; ++sidx) {
|
||||
const LoopStep& stp = commit_plan[sidx];
|
||||
const int64_t l = stp.layer;
|
||||
#else
|
||||
for (int64_t l = lb_; l < le_ && ok; ++l) {
|
||||
#endif
|
||||
const LayerView v(*wt_, l);
|
||||
if (is_qsa_layer(g, l) && qsa_batched) {
|
||||
++qsa_index;
|
||||
} else if (!is_qsa_layer(g, l) && gdn_batched) {
|
||||
++gdn_index;
|
||||
} else if (!is_qsa_layer(g, l)) {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const int64_t gi = stp.gdn_slot;
|
||||
#endif
|
||||
if (MT > kVerifyMaxT) { // gdn_step_norm_multi takes kVerifyMaxT tokens; gdn_commit_layers takes more
|
||||
err = "verify commit: windows past " + std::to_string(kVerifyMaxT) + " tokens (--lookup-max-t) need "
|
||||
"the batched GDN commit (unset STRATA_COMMIT_BATCH=0, STRATA_GDN_SPLIT, STRATA_GDN_COMMIT_SPLIT=0)";
|
||||
@@ -3792,28 +4033,57 @@ bool Verifier::capture_commit(std::string& err) {
|
||||
}
|
||||
const WeightRef* wnm = need(v, "ssm_norm.weight", err);
|
||||
if (!wnm) { ok = false; break; }
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
float* state = ss.gdn_state + (size_t) (gi - ss.gdn_ord0) * gdn_floats;
|
||||
#else
|
||||
float* state = ss.gdn_state + (size_t) (gdn_index - ss.gdn_ord0) * gdn_floats;
|
||||
#endif
|
||||
float* conv = state + (uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const float* qkv = qkv_L_ + (size_t) gi * MT * C;
|
||||
#else
|
||||
const float* qkv = qkv_L_ + (size_t) gdn_index * MT * C;
|
||||
#endif
|
||||
gdn_conv_commit(conv, qkv, (int) C, commit_, cs_);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
gdn_step_norm_multi(state, h_L_ + (size_t) gi * MT * C, (int) C, gate_L_ + (size_t) gi * MT * HV,
|
||||
beta_L_ + (size_t) gi * MT * HV, z_, (const float*) wnm->data, EPS, y_dummy_,
|
||||
#else
|
||||
gdn_step_norm_multi(state, h_L_ + (size_t) gdn_index * MT * C, (int) C, gate_L_ + (size_t) gdn_index * MT * HV,
|
||||
beta_L_ + (size_t) gdn_index * MT * HV, z_, (const float*) wnm->data, EPS, y_dummy_,
|
||||
#endif
|
||||
(int) g.ssm_k_heads, (int) HV, (int) MT, commit_, cs_,
|
||||
(int) MT); // S26: t_out_begin = MT - the replay's outputs (y_dummy_) are never read
|
||||
++gdn_index;
|
||||
} else {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const int64_t qi = stp.qsa_slot;
|
||||
const QsaState& st = ss.qsa_states[qi];
|
||||
#else
|
||||
const QsaState& st = ss.qsa_states[qsa_index];
|
||||
#endif
|
||||
const WeightRef* wikn = need(v, "indexer.k_norm.weight", err);
|
||||
if (!wikn) { ok = false; break; }
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
copy_from_mapped(st.idx_tail, tail_snap_ + (size_t) qi * TS, TS, cs_);
|
||||
#else
|
||||
copy_from_mapped(st.idx_tail, tail_snap_ + (size_t) qsa_index * TS, TS, cs_);
|
||||
#endif
|
||||
const QsaIndexerBuffers ib{st.idx_tail, st.idx_dead, st.idx_pooled, st.idx_block_pos};
|
||||
if (!g_no_batch_kv) {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
native_qsa_indexer_append_steps(idx_raw_L_ + (size_t) qi * MT * ID, commit_ + 2, 1, (int) MT, 0,
|
||||
#else
|
||||
native_qsa_indexer_append_steps(idx_raw_L_ + (size_t) qsa_index * MT * ID, commit_ + 2, 1, (int) MT, 0,
|
||||
#endif
|
||||
(const float*) wikn->data, EPS, ib, s, st.max_cells, rope_scaling(), cs_);
|
||||
} else {
|
||||
for (int64_t t = 0; t < MT; ++t)
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
native_qsa_indexer_append(idx_raw_L_ + (size_t) (qi * MT + t) * ID, commit_ + 2 + t, 0,
|
||||
#else
|
||||
native_qsa_indexer_append(idx_raw_L_ + (size_t) (qsa_index * MT + t) * ID, commit_ + 2 + t, 0,
|
||||
#endif
|
||||
(const float*) wikn->data, EPS, ib, s, st.max_cells,
|
||||
rope_scaling(), cs_);
|
||||
}
|
||||
@@ -3857,6 +4127,7 @@ void Verifier::stage_inputs(int T, const int32_t* tokens, int64_t pos0) {
|
||||
for (int64_t h = 0; h < g.n_head_kv; ++h) pk[t * g.n_head_kv + h] = pos_t;
|
||||
for (int64_t h = 0; h < g.idx_q_heads; ++h) pi[t * g.idx_q_heads + h] = pos_t;
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (h_cd_omega_ != nullptr) {
|
||||
const auto& la = strata::kernels::loop_adapter();
|
||||
const float om = (la.loaded() && !bypass_loaded_) ? (loop_req_set_ ? loop_cd_omega_ : la.cd_omega) : 0.0f;
|
||||
@@ -3864,6 +4135,7 @@ void Verifier::stage_inputs(int T, const int32_t* tokens, int64_t pos0) {
|
||||
strata::kernels::loop_cd_fill_omega(tokens, T, om, th, la.think_start_id, la.think_end_id,
|
||||
loop_cd_in_thinking_, h_cd_omega_);
|
||||
}
|
||||
#endif
|
||||
*(volatile uint32_t*) h_seq_ = 0;
|
||||
*(volatile uint32_t*) h_flag_ = 0;
|
||||
*(volatile uint32_t*) h_flagA_ = 0;
|
||||
@@ -3993,7 +4265,11 @@ bool Verifier::handoff_failed(std::string& err) const {
|
||||
}
|
||||
|
||||
bool Verifier::chain_overlap() const {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
return next_ != nullptr && !cyclic() && hflag_out_h_ != nullptr && hflag_out_d_ != nullptr && hcount_d_ != nullptr &&
|
||||
#else
|
||||
return next_ != nullptr && hflag_out_h_ != nullptr && hflag_out_d_ != nullptr && hcount_d_ != nullptr &&
|
||||
#endif
|
||||
next_->hflag_in_h_ == hflag_out_h_ && next_->device_ != device_ && !next_->ple_stage();
|
||||
}
|
||||
|
||||
@@ -4111,6 +4387,7 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
|
||||
const OnDevice on_device(device_);
|
||||
last_batch_ = false;
|
||||
split_logits_materialized_ = false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// a cyclic split: which hop of the itinerary this call runs (the caller enters at the first)
|
||||
const int hop = (cyclic() && !bypass_loaded_) ? (cyc_entered_ ? cyc_hop_ : 0) : -1;
|
||||
cyc_entered_ = false;
|
||||
@@ -4123,6 +4400,7 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
|
||||
cyc_select(cyc_visit_);
|
||||
}
|
||||
const LoopVisit* cvis = hop >= 0 ? &cyc_visits_[(size_t) cyc_visit_] : nullptr;
|
||||
#endif
|
||||
if (T < 1 || T > max_t_) { err = "verify: window size out of range"; return false; }
|
||||
// a layer split's hand-off buffers hold kVerifyMaxT rows
|
||||
if (T > kVerifyMaxT && (next_ != nullptr || lb_ > 0)) { err = "verify: windows past 8 tokens need one stage"; return false; }
|
||||
@@ -4150,7 +4428,11 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
|
||||
const Clock::time_point t0 = Clock::now();
|
||||
if (!pre && !staged_) stage_inputs(T, tokens, pos0);
|
||||
staged_ = false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool do_ple = ss.ple.ready() && ple_stage() && (cvis == nullptr || cvis->ple);
|
||||
#else
|
||||
const bool do_ple = ss.ple.ready() && ple_stage();
|
||||
#endif
|
||||
uint32_t ple_rows[kVerifyLongT * PLE_N_HEADS];
|
||||
if (do_ple) {
|
||||
int32_t prev[2] = {ss.ple_prev[0], ss.ple_prev[1]};
|
||||
@@ -4297,10 +4579,14 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
|
||||
volatile uint32_t* const flag = h_flag_;
|
||||
const int G = groups_[T] > 0 ? groups_[T] : 1;
|
||||
const int gtb[2] = {0, (T + 1) / 2}, gte[2] = {G == 2 ? (T + 1) / 2 : T, T};
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const auto run_plan = cvis != nullptr ? cvis->steps
|
||||
: strata::kernels::build_loop_schedule(g.n_layers, g.qsa_interval, lb_, le_,
|
||||
bypass_loaded_);
|
||||
const int64_t steps = (int64_t) run_plan.size() * G;
|
||||
#else
|
||||
const int64_t steps = (le_ - lb_) * G;
|
||||
#endif
|
||||
if (ar_on() && !test_stall) {
|
||||
if (flag_raised) {
|
||||
// up before the launch (early_ple_flag_mode)
|
||||
@@ -4318,7 +4604,11 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
|
||||
// PLE rows already gathered before graph and peer launch
|
||||
} else
|
||||
for (int64_t k = 0; k < steps; ++k) {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const int64_t l = run_plan[(size_t) (k / G)].layer;
|
||||
#else
|
||||
const int64_t l = lb_ + k / G;
|
||||
#endif
|
||||
const int grp = (int) (k % G);
|
||||
const uint32_t want = (uint32_t) (k + 1);
|
||||
const Clock::time_point a = Clock::now();
|
||||
@@ -4513,6 +4803,7 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
|
||||
// own (a captured kernel would replay the same draws forever). Row t's draw is Philox(seed, pos0 + t): tied to
|
||||
// the POSITION it samples, not to how the text was cut into windows, so a seed replays the same text whatever
|
||||
// the drafts were. Exact: a rejected row's draw is discarded, and no kept decision depends on a reused draw.
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (cvis != nullptr && !cvis->head) { // a cyclic visit before the last: the next hop reads its hand-off
|
||||
if (cvis->owner_first) ++windows;
|
||||
Verifier* nv = (*cyc_hops_)[(size_t) hop + 1].ver;
|
||||
@@ -4523,6 +4814,9 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
|
||||
return true;
|
||||
}
|
||||
if (cvis == nullptr && le_ < g.n_layers) { // a layer split's earlier stage: the hand-off is written (synced above)
|
||||
#else
|
||||
if (le_ < g.n_layers) { // a layer split's earlier stage: the hand-off is written (synced above)
|
||||
#endif
|
||||
++windows;
|
||||
if (next_ != nullptr && !next_->run(T, tokens, pos0, pool, next_user_, out, err)) return false;
|
||||
// a layer split's first stage owns STRATA_TAP_FILE: it writes once the rest of the chain has run
|
||||
@@ -4631,13 +4925,19 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
|
||||
VDBG("window done\n");
|
||||
if (megakernel_stall_take()) {
|
||||
megakernel_off();
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (hop >= 0) { // the replay is this hop's, not the itinerary's first
|
||||
cyc_hop_ = hop;
|
||||
cyc_entered_ = true;
|
||||
}
|
||||
#endif
|
||||
return run(T, tokens, pos0, pool, user, out, err);
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (cvis == nullptr || cvis->owner_first) ++windows;
|
||||
#else
|
||||
++windows;
|
||||
#endif
|
||||
progress_at("decode");
|
||||
progress_beat();
|
||||
return true;
|
||||
@@ -4658,14 +4958,17 @@ void Verifier::megakernel_off() {
|
||||
const OnDevice on(device_);
|
||||
if (cs_) (void) cudaStreamSynchronize(cs_);
|
||||
mk_in_graph_ = false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool was_bypass = bypass_loaded_;
|
||||
if (was_bypass) select_loop_bypass(false);
|
||||
#endif
|
||||
for (auto& row : exec_)
|
||||
for (auto& e : row)
|
||||
if (e != nullptr) { (void) cudaGraphExecDestroy(e); e = nullptr; }
|
||||
for (auto& row : exec_nr_)
|
||||
for (auto& e : row)
|
||||
if (e != nullptr) { (void) cudaGraphExecDestroy(e); e = nullptr; }
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
for (size_t v = 0; v < cyc_graphs_.size(); ++v) {
|
||||
if ((int) v == cyc_loaded_) { cyc_graphs_[v] = CycGraphs{}; continue; }
|
||||
for (auto* tab : {&cyc_graphs_[v].exec, &cyc_graphs_[v].exec_nr})
|
||||
@@ -4678,6 +4981,7 @@ void Verifier::megakernel_off() {
|
||||
for (auto& e : row)
|
||||
if (e != nullptr) { (void) cudaGraphExecDestroy(e); e = nullptr; }
|
||||
if (was_bypass) select_loop_bypass(true);
|
||||
#endif
|
||||
}
|
||||
|
||||
// set_pcie_balance: each stamped step's times into the model's fits (PcieModel::sample), solved once a window.
|
||||
@@ -4938,11 +5242,13 @@ bool Verifier::commit(int n_keep, std::string& err) {
|
||||
ss_->ple_prev[0] = ss_->ple_prev[1];
|
||||
ss_->ple_prev[1] = last_tokens_[t];
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (h_cd_omega_ != nullptr) {
|
||||
const auto& la = strata::kernels::loop_adapter();
|
||||
loop_cd_in_thinking_ = strata::kernels::loop_think_state_after(
|
||||
last_tokens_, n_keep, la.think_start_id, la.think_end_id, loop_cd_in_thinking_);
|
||||
}
|
||||
#endif
|
||||
ms_commit += ms_since(t0);
|
||||
return next_ == nullptr || next_->commit(n_keep, err);
|
||||
}
|
||||
@@ -4981,10 +5287,12 @@ bool Verifier::init_slots(const std::vector<SessionState*>& slots, std::string&
|
||||
err = "verify: init_slots needs at least one session";
|
||||
return false;
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (cyclic()) {
|
||||
err = "verify: batch windows do not run on a cyclic layer split";
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
for (SessionState* x : slots) {
|
||||
if (x == nullptr || x->layer_lo != ss_->layer_lo || x->layer_hi != ss_->layer_hi ||
|
||||
x->max_cells != ss_->max_cells || x->gdn_ord0 != ss_->gdn_ord0 || x->qsa_ord0 != ss_->qsa_ord0) {
|
||||
@@ -5451,6 +5759,7 @@ bool Verifier::drop_window_graphs(int& dropped, std::string& err) {
|
||||
err = "verify: synchronizing before dropping the window graphs failed";
|
||||
return false;
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool was_bypass = bypass_loaded_;
|
||||
if (was_bypass) select_loop_bypass(false);
|
||||
auto drop_bank = [&](CycGraphs& bank, bool destroy) {
|
||||
@@ -5469,6 +5778,7 @@ bool Verifier::drop_window_graphs(int& dropped, std::string& err) {
|
||||
bank.launched[s][t] = bank.launched_nr[s][t] = false;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
for (int s = 0; s < 2; ++s)
|
||||
for (int t = 0; t <= strata::kernels::kVerifyLongT; ++t) {
|
||||
for (cudaGraphExec_t* e : {&exec_[s][t], &exec_nr_[s][t]})
|
||||
@@ -5479,9 +5789,11 @@ bool Verifier::drop_window_graphs(int& dropped, std::string& err) {
|
||||
}
|
||||
launched_[s][t] = launched_nr_[s][t] = false; // the next graph of that size is a cold first launch again
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
for (size_t v = 0; v < cyc_graphs_.size(); ++v) drop_bank(cyc_graphs_[v], (int) v != cyc_loaded_);
|
||||
drop_bank(bypass_graphs_, true);
|
||||
if (was_bypass) select_loop_bypass(true);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,9 @@
|
||||
// BF16 path affects only the scalar gate: it reads the original F32 input and uses pinned CUDA MMVF plus
|
||||
// an FP32 sigmoid. Native projection overrides independently select CUDA Q8_1 MMVQ and FP32 SwiGLU.
|
||||
#include "strata/kernels/shared_expert.hpp"
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
#include "strata/kernels/loop_adapter.hpp"
|
||||
#endif
|
||||
#include "strata/kernels/fuse_decode.hpp"
|
||||
#include "strata/kernels/bf16_gemv.hpp"
|
||||
#include "strata/kernels/bf16_bits.hpp"
|
||||
@@ -216,10 +218,16 @@ bool fused_swiglu_q81_enabled() {
|
||||
|
||||
void shared_expert_multi(int n_tok, const float* x, const uint16_t* x_bf16, const NativeSharedWeights& nw,
|
||||
const uint16_t* gate_inp_bf16, float* gate, float* up, float* g, float* out, int64_t n_embd,
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
int64_t n_ff, void* stream, const void* x_q8_1_ready, int lfuse, int loop_pass,
|
||||
int64_t loop_layer) {
|
||||
#else
|
||||
int64_t n_ff, void* stream, const void* x_q8_1_ready, int lfuse) {
|
||||
#endif
|
||||
const bool gate_deferred = (lfuse & 1) != 0, pair = (lfuse & 2) != 0;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool has_sh_lora = loop_pass >= 0 && loop_layer >= 0 && loop_adapter().has_sh_lora(loop_pass, loop_layer);
|
||||
#endif
|
||||
if (n_tok < 1 || n_tok > kVerifyMaxT || !nw.q8_1 || !nw.gate_data || !nw.up_data || !nw.down_data || !stream)
|
||||
throw std::invalid_argument("shared_expert_multi: needs 1..8 tokens (16 in a wide build), native weights, scratch and a stream");
|
||||
cudaStream_t cs = (cudaStream_t) stream;
|
||||
@@ -245,14 +253,20 @@ void shared_expert_multi(int n_tok, const float* x, const uint16_t* x_bf16, cons
|
||||
native_mmvq(nw.gate_type, nw.gate_data, x_q8_1, gate, (int) n_embd, (int) n_ff, n_tok, stream);
|
||||
native_mmvq(nw.up_type, nw.up_data, x_q8_1, up, (int) n_embd, (int) n_ff, n_tok, stream);
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (has_sh_lora) {
|
||||
loop_lora_add(x, (int) n_embd, gate, (int) n_ff, loop_pass, loop_layer, LoopTarget::kShGate, n_tok, stream);
|
||||
loop_lora_add(x, (int) n_embd, up, (int) n_ff, loop_pass, loop_layer, LoopTarget::kShUp, n_tok, stream);
|
||||
}
|
||||
const bool has_down_lora =
|
||||
has_sh_lora && loop_adapter().has_lora(loop_pass, loop_layer, LoopTarget::kShDown);
|
||||
#endif
|
||||
const int n = (int) (n_ff * n_tok);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (fused_swiglu_q81_enabled() && !has_down_lora) {
|
||||
#else
|
||||
if (fused_swiglu_q81_enabled()) {
|
||||
#endif
|
||||
native_swiglu_quantize_q8_1(gate, up, nw.q8_1, (int) n_ff, n_tok, stream);
|
||||
} else {
|
||||
native_swiglu_kernel<<<(unsigned) ((n + THREADS - 1) / THREADS), THREADS, 0, cs>>>(gate, up, gate, n);
|
||||
@@ -264,9 +278,11 @@ void shared_expert_multi(int n_tok, const float* x, const uint16_t* x_bf16, cons
|
||||
} else {
|
||||
native_mmvq(nw.down_type, nw.down_data, nw.q8_1, out, (int) n_ff, (int) n_embd, n_tok, stream);
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (has_down_lora) {
|
||||
loop_lora_add(gate, (int) n_ff, out, (int) n_embd, loop_pass, loop_layer, LoopTarget::kShDown, n_tok, stream);
|
||||
}
|
||||
#endif
|
||||
if (gate_deferred) { // the caller computed g (raw) with the router and scales `out` in the combine
|
||||
const cudaError_t e = cudaGetLastError();
|
||||
if (e != cudaSuccess) throw std::runtime_error(std::string("shared_expert_multi: ") + cudaGetErrorString(e));
|
||||
|
||||
@@ -59,7 +59,9 @@
|
||||
#include "strata/kernels/qsa.hpp"
|
||||
#include "strata/kernels/kv_stream.hpp"
|
||||
#include "strata/kernels/cvec.hpp"
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
#include "strata/kernels/loop_adapter.hpp"
|
||||
#endif
|
||||
#include "strata/kernels/kv_q4.hpp"
|
||||
#include "strata/core/layer.hpp"
|
||||
#include "strata/core/native_dense.hpp"
|
||||
@@ -1119,6 +1121,7 @@ struct Alloc {
|
||||
|
||||
} // namespace
|
||||
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// A cyclic layer split (Prefill::set_cyclic): the itinerary, every stage's path, and a run's progress - per visit, the
|
||||
// chunks it has finished. Visit g of chunk c waits for visit g - 1 of chunk c (its rows), and a visit that hands rows
|
||||
// on waits until the readers of what its stage handed on two chunks ago (the same buffer) have finished.
|
||||
@@ -1133,6 +1136,7 @@ struct Prefill::Cyc {
|
||||
std::string err;
|
||||
};
|
||||
|
||||
#endif
|
||||
// Step 4 of the prompt-speed plan: the experts the arena could not pin (a third of the streamed ones on IQ3_S) are
|
||||
// copied into pinned buffers by these threads, ahead of the launches. Copied in line by the launching thread they
|
||||
// left the GPU without queued work while each ~2 MB memcpy ran (~15 s of a 32K prompt on IQ3_S). Job j - a layer's
|
||||
@@ -1569,8 +1573,10 @@ struct Prefill::Impl {
|
||||
std::vector<void*> owned;
|
||||
// chunk buffers
|
||||
float *emb = nullptr, *R = nullptr, *xn = nullptr, *lo = nullptr, *gated = nullptr, *inj = nullptr;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
float *R_anchor = nullptr, *R_mprev = nullptr, *R_m0 = nullptr; // Looped adapter residual checkpoints
|
||||
float *cd_omega = nullptr; // Looped adapter per-token LoopCD omega [T]
|
||||
#endif
|
||||
float* grs = nullptr; // F-1: the hyper-connection read's row scales (T x 4)
|
||||
uint16_t *xn16 = nullptr, *lo16 = nullptr;
|
||||
float* mixed = nullptr;
|
||||
@@ -1719,8 +1725,10 @@ struct Prefill::Impl {
|
||||
// layer split: the device, and the hand-off to the next stage (two pinned chunk buffers, used in turn)
|
||||
int device = -1;
|
||||
float* hand[2] = {};
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
float* hand_mp[2] = {}; // a cyclic split: the m_prev rows relayed beside them (set_cyclic)
|
||||
int64_t hand_rows = 0; // the rows each hand-off buffer holds
|
||||
#endif
|
||||
// STRATA_HANDOFF_PIPE: per hand-off buffer, "the chunk's work is done" and one "landed" event per piece
|
||||
int hand_pieces = 0;
|
||||
cudaEvent_t hand_done[2] = {};
|
||||
@@ -1861,7 +1869,9 @@ void Prefill::release() {
|
||||
}
|
||||
for (int b = 0; b < 2; ++b) {
|
||||
if (impl_->hand[b]) cudaFreeHost(impl_->hand[b]);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (impl_->hand_mp[b]) cudaFreeHost(impl_->hand_mp[b]);
|
||||
#endif
|
||||
if (impl_->hand_done[b]) cudaEventDestroy(impl_->hand_done[b]);
|
||||
for (cudaEvent_t& e : impl_->hand_ev[b])
|
||||
if (e) cudaEventDestroy(e);
|
||||
@@ -2505,6 +2515,7 @@ bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, co
|
||||
}
|
||||
// the CPU share's staged-chunk limit, before the ring is sized below (a caller that sizes loans first armed it)
|
||||
arm_cpu_share(cpu_pool_ != nullptr);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// a cyclic split: a stage hands rows on from each visit but the final one (and relays m_prev where one says so)
|
||||
bool cyc_writes = false, cyc_relays = false;
|
||||
if (cyc_ != nullptr)
|
||||
@@ -2514,16 +2525,21 @@ bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, co
|
||||
cyc_relays = cyc_relays || v.mprev_out;
|
||||
}
|
||||
for (int b = 0; (next_ != nullptr || cyc_writes) && b < 2; ++b)
|
||||
#else
|
||||
for (int b = 0; next_ != nullptr && b < 2; ++b)
|
||||
#endif
|
||||
if (!m.hand[b] && cudaHostAlloc((void**) &m.hand[b], (size_t) chunk * D * 4, cudaHostAllocPortable) != cudaSuccess) {
|
||||
err = "prefill: the layer split's hand-off buffers";
|
||||
return false;
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
for (int b = 0; cyc_relays && b < 2; ++b)
|
||||
if (!m.hand_mp[b] && cudaHostAlloc((void**) &m.hand_mp[b], (size_t) chunk * D * 4, cudaHostAllocPortable) != cudaSuccess) {
|
||||
err = "prefill: the cyclic layer split's m_prev hand-off buffers";
|
||||
return false;
|
||||
}
|
||||
m.hand_rows = chunk;
|
||||
#endif
|
||||
if (next_ != nullptr && handoff_pipe_pieces() > 0 && m.hand_pieces == 0) { // events only: no device memory
|
||||
bool ok = true;
|
||||
auto make = [](cudaEvent_t& e) { return e != nullptr || cudaEventCreateWithFlags(&e, cudaEventDisableTiming) == cudaSuccess; };
|
||||
@@ -2798,6 +2814,7 @@ bool Prefill::carve(size_t T, void* alloc) {
|
||||
core::SessionState& ss = *m.ss;
|
||||
bool ok = true;
|
||||
m.emb = o.take<float>(T * N, ok); m.R = o.take<float>(T * D, ok);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (strata::kernels::loop_adapter().loaded()) {
|
||||
m.R_anchor = o.take<float>(T * D, ok);
|
||||
m.R_mprev = o.take<float>(T * D, ok);
|
||||
@@ -2806,6 +2823,7 @@ bool Prefill::carve(size_t T, void* alloc) {
|
||||
} else {
|
||||
m.R_anchor = m.R_mprev = m.R_m0 = m.cd_omega = nullptr;
|
||||
}
|
||||
#endif
|
||||
m.xn = gr_unfused() ? o.take<float>(T * D, ok) : nullptr; // F-1: not needed (gr_mix_r reads R)
|
||||
m.grs = o.take<float>(T * HC, ok);
|
||||
const bool hcr = pf_hc_region(); // STRATA_PF_HC_REGION: xn16 / lo / lo16 / gated (and their _lo) are taken in the region
|
||||
@@ -3776,13 +3794,19 @@ uint64_t Prefill::bytes_needed_impl(const core::ModelGeometry& g, const core::Se
|
||||
// still bounds-checks - but it under-sizes every loan, so every chunk the scan picks is one step smaller.
|
||||
if (ring_bytes_on()) {
|
||||
f(T * N); f(T * D);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (strata::kernels::loop_adapter().loaded()) { f(T * D); f(T * D); f(T * D); f(T); }
|
||||
#endif
|
||||
if (gr_unfused()) f(T * D);
|
||||
f(T * HC);
|
||||
} else { // STRATA_RING_BYTES=0: 0.1.39's count
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
f(T * N); f(T * D);
|
||||
if (strata::kernels::loop_adapter().loaded()) { f(T * D); f(T * D); f(T * D); f(T); }
|
||||
f(T * D);
|
||||
#else
|
||||
f(T * N); f(T * D); f(T * D);
|
||||
#endif
|
||||
}
|
||||
const bool hcr = pf_hc_region();
|
||||
if (!hcr) { o.take<uint16_t>(T * (D + (hc_pad() ? XN_PAD : 0)), ok); f(T * LR); o.take<uint16_t>(T * LR, ok); f(T * D); }
|
||||
@@ -4345,7 +4369,11 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
// pool would decide which experts the CPU computed (other bits) - timing, where the chunks are a function of n
|
||||
const bool pipe_chunked = hand_in_ == nullptr ? (forced > 0 ? forced_planned : planned_C > 0 && C < std::min(n, m.T))
|
||||
: pipe_chunked_;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool helped = single_chunk && cyc_vis_ == nullptr && bind_stage_helper(n);
|
||||
#else
|
||||
const bool helped = single_chunk && bind_stage_helper(n);
|
||||
#endif
|
||||
if (helped) std::swap(m.pp, m.help_pp);
|
||||
struct HelpScope {
|
||||
Impl& m;
|
||||
@@ -4369,7 +4397,11 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
// The PLE rows of a chunk are read from the model file on the host (an SSD read per missed row): the chunk
|
||||
// after this one is read on a thread while the GPU runs this one, into the other of two buffers. The rows
|
||||
// depend only on the tokens (the two before a position name its n-grams), so this is the same data.
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool ple_on = ss.ple.ready() && LB <= 1 && 1 < LE && (cyc_vis_ == nullptr || cyc_vis_->ple);
|
||||
#else
|
||||
const bool ple_on = ss.ple.ready() && LB <= 1 && 1 < LE;
|
||||
#endif
|
||||
// the PLE block batched over the chunk: the pinned postops and a BF16 or GGUF-native key (else token by token);
|
||||
// STRATA_PLE_BATCH=0 keeps the per-token block (the A/B)
|
||||
const bool ple_batch_env = ple_batch_enabled();
|
||||
@@ -4421,6 +4453,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
for (size_t i = 0; i < tap_local_.size(); ++i) tap_slot[(size_t) tap_local_[i]] = (int) i;
|
||||
}
|
||||
#endif
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const auto& la_pf = strata::kernels::loop_adapter();
|
||||
const bool loop_on = la_pf.loaded() && !loop_bypass_;
|
||||
const float eff_cd_omega = loop_on ? (loop_req_set_ ? loop_cd_omega_ : la_pf.cd_omega) : 0.0f;
|
||||
@@ -4432,6 +4465,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
la_pf.think_end_id, loop_cd_in_thinking_, cd_omega_host.data());
|
||||
}
|
||||
bool chunk_think_state = loop_cd_in_thinking_;
|
||||
#endif
|
||||
for (int64_t c0 = 0; c0 < n; c0 += C) {
|
||||
if (should_stop && should_stop()) { err = "cancelled"; return false; }
|
||||
// the first stage: end the run before this chunk (every chunk before it is handed on; the caller's drain
|
||||
@@ -4475,7 +4509,11 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
#else
|
||||
const int64_t tap_t0 = 0, tap_rows = 0;
|
||||
#endif
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (cyc_vis_ == nullptr || cyc_vis_->owner_first) ++stats_.chunks;
|
||||
#else
|
||||
++stats_.chunks;
|
||||
#endif
|
||||
pt.mark(kPfStart, cs);
|
||||
const auto tsetup = Clock::now();
|
||||
// ---- embeddings, broadcast to the four streams - or, in a later stage of a layer split, the rows the
|
||||
@@ -4518,6 +4556,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// a cyclic visit on a later pass: the previous pass's core output - the handed-in rows themselves on the wrap
|
||||
// into the core's first stage, else relayed beside them (the workspace entry overwrites R: never an alias)
|
||||
if (cyc_vis_ != nullptr && cyc_vis_->mprev_in != 0 &&
|
||||
@@ -4527,6 +4566,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
err = "prefill: the cyclic layer split's m_prev upload failed";
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
// C-4: the whole chunk's rows in one gather (the same per-element arithmetic as the per-token path, so the
|
||||
// same bits); a chunk with picture rows, or a token outside the table, takes the per-token path
|
||||
bool batched = hand_in_ == nullptr && m.tok_dev != nullptr;
|
||||
@@ -4656,6 +4696,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool chunk_think_before = chunk_think_state;
|
||||
if (next_ != nullptr && cyc_vis_ == nullptr && loop_on && eff_cd_thinking) {
|
||||
chunk_think_state = strata::kernels::loop_think_state_after(
|
||||
@@ -4672,11 +4713,16 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
const auto loop_plan =
|
||||
cyc_vis_ != nullptr ? cyc_vis_->steps
|
||||
: strata::kernels::build_loop_schedule(g.n_layers, g.qsa_interval, LB, LE, loop_bypass_);
|
||||
#endif
|
||||
int64_t qsa_index = 0, gdn_index = 0;
|
||||
for (int64_t l = 0; l < LB; ++l) (core::is_qsa_layer(g, l) ? qsa_index : gdn_index) += 1;
|
||||
int64_t kv_pending = -1;
|
||||
auto kv_prefetch_after = [&](int64_t layer, int64_t ordinal) -> bool {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (!kv_prefetch || p0 <= 0 || loop_on) return true;
|
||||
#else
|
||||
if (!kv_prefetch || p0 <= 0) return true;
|
||||
#endif
|
||||
if (kv_pending >= 0) return true; // one staging pool: the pending prefetch has not been consumed yet
|
||||
for (int64_t l = layer; l < LE; ++l) {
|
||||
if (!core::is_qsa_layer(g, l)) continue;
|
||||
@@ -4736,9 +4782,17 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
++P.p_issued;
|
||||
}
|
||||
};
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
auto p_release_to = [&](size_t sidx_arg, int32_t e_stop, cudaStream_t ps) {
|
||||
#else
|
||||
auto p_release_to = [&](int64_t l, int32_t e_stop, cudaStream_t ps) {
|
||||
#endif
|
||||
PeerPrefill& P = *m.pp;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
while (P.pk < P.pseq_start[sidx_arg + 1] && P.pseq[P.pk].e < e_stop) {
|
||||
#else
|
||||
while (P.pk < P.pseq_start[(size_t) l + 1] && P.pseq[P.pk].e < e_stop) {
|
||||
#endif
|
||||
cudaEventRecord(P.pused[P.pk % (size_t) P.RP], ps);
|
||||
++P.pk;
|
||||
p_issue_until(P.pk + (size_t) P.RP);
|
||||
@@ -4749,11 +4803,19 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
std::vector<size_t> seq_start;
|
||||
size_t issued = 0, consumed = 0;
|
||||
if (stream_all) {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
seq_start.assign(loop_plan.size() + 1, 0);
|
||||
#else
|
||||
seq_start.assign((size_t) g.n_layers + 1, 0);
|
||||
#endif
|
||||
std::vector<Stager::Job> js;
|
||||
if (ps_on) {
|
||||
m.pp->pseq.clear();
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
m.pp->pseq_start.assign(loop_plan.size() + 1, 0);
|
||||
#else
|
||||
m.pp->pseq_start.assign((size_t) g.n_layers + 1, 0);
|
||||
#endif
|
||||
m.pp->ps_flag.assign((size_t) g.n_layers * m.g->n_expert, 0);
|
||||
m.pp->p_issued = m.pp->pk = 0;
|
||||
}
|
||||
@@ -4762,14 +4824,24 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
// transfer plan is where the avoidable cost lives (the research handoff's "expert transfers must be
|
||||
// skipped too"). `kva_skips_layer` is the one place the two conditions meet.
|
||||
auto kva_skips_layer = [&](int64_t l) {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
return kva_approx && !loop_on && l >= kva_split &&
|
||||
#else
|
||||
return kva_approx && l >= kva_split &&
|
||||
#endif
|
||||
(kva.only == KvaConfig::Only::All ||
|
||||
(kva.only == KvaConfig::Only::Gdn) == !core::is_qsa_layer(g, l));
|
||||
};
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
for (size_t si = 0; si < loop_plan.size(); ++si) {
|
||||
const int64_t l = loop_plan[si].layer;
|
||||
seq_start[si] = seq.size();
|
||||
if (ps_on) m.pp->pseq_start[si] = m.pp->pseq.size();
|
||||
#else
|
||||
for (int64_t l = LB; l < LE; ++l) {
|
||||
seq_start[(size_t) l] = seq.size();
|
||||
if (ps_on) m.pp->pseq_start[(size_t) l] = m.pp->pseq.size();
|
||||
#endif
|
||||
if (kva_skips_layer(l)) continue; // no transfer for a layer whose MoE half is skipped
|
||||
for (int32_t e = 0; e < m.g->n_expert; ++e) {
|
||||
if (m.host_res && m.cache && m.host_res[(size_t) l * m.g->n_expert + e] >= 0) continue;
|
||||
@@ -4799,8 +4871,15 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
seq.push_back({(int32_t) l, e, b, job});
|
||||
}
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
seq_start[loop_plan.size()] = seq.size();
|
||||
if (ps_on) m.pp->pseq_start[loop_plan.size()] = m.pp->pseq.size();
|
||||
#else
|
||||
for (int64_t l = LE; l <= g.n_layers; ++l) {
|
||||
seq_start[(size_t) l] = seq.size();
|
||||
if (ps_on) m.pp->pseq_start[(size_t) l] = m.pp->pseq.size();
|
||||
}
|
||||
#endif
|
||||
m.stager->start(std::move(js));
|
||||
}
|
||||
struct StagerDone {
|
||||
@@ -4856,7 +4935,11 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
// before the issuer thread, so they outlive it on every return (IssuerJoin joins it first).
|
||||
static const bool rs_env = routed_stream::enabled_from(std::getenv("STRATA_PREFILL_ROUTED_STREAM"));
|
||||
static const int rs_depth = routed_stream::depth_from(std::getenv("STRATA_PREFILL_ROUTED_STREAM_DEPTH"));
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool rs_on = !loop_on && rs_env && routed_stream::eligible(
|
||||
#else
|
||||
const bool rs_on = rs_env && routed_stream::eligible(
|
||||
#endif
|
||||
stream_all, issuer_on, m.pp != nullptr, copy_event_every, copy_wait_dedupe);
|
||||
routed_stream::RouteGate rs_gate;
|
||||
size_t rs_dropped = 0;
|
||||
@@ -5054,11 +5137,16 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
const char* v = std::getenv("STRATA_STOP_LAYERS");
|
||||
return v == nullptr || std::atoi(v) != 0;
|
||||
}();
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
for (size_t sidx = 0; sidx < loop_plan.size(); ++sidx) {
|
||||
const strata::kernels::LoopStep& stp = loop_plan[sidx];
|
||||
const int64_t l = stp.layer;
|
||||
const int pass = stp.pass;
|
||||
if (sidx > 0 && stop_in_chunk && should_stop && should_stop()) {
|
||||
#else
|
||||
for (int64_t l = LB; l < LE; ++l) {
|
||||
if (l > LB && stop_in_chunk && should_stop && should_stop()) {
|
||||
#endif
|
||||
a_stop.store(true, std::memory_order_release);
|
||||
if (issuer.joinable()) issuer.join();
|
||||
if (stream_all) m.stager->finish();
|
||||
@@ -5074,12 +5162,20 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
return false;
|
||||
}
|
||||
core::progress_beat(); // the serve watchdog: a prompt chunk of 8192 tokens is still moving
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (sidx > 0) pf_step("reading the prompt (batched, step sync): the experts and the rest of layer", loop_plan[sidx - 1].layer);
|
||||
#else
|
||||
if (l > LB) pf_step("reading the prompt (batched, step sync): the experts and the rest of layer", l - 1);
|
||||
#endif
|
||||
core::progress_at("reading the prompt (batched): layer", l, p0); // #251: a stall names layer and chunk
|
||||
const core::LayerView v(*m.wt, l);
|
||||
// STRATA_KVA: `kva_layer` means this layer's output has no consumer left for this chunk, so only its
|
||||
// state advances; `kva_subst` means the block input itself is replayed from a capture (oracle mode).
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool kva_layer = kva_approx && !loop_on && l >= kva_split &&
|
||||
#else
|
||||
const bool kva_layer = kva_approx && l >= kva_split &&
|
||||
#endif
|
||||
(kva.only == KvaConfig::Only::All ||
|
||||
(kva.only == KvaConfig::Only::Gdn) == !core::is_qsa_layer(g, l));
|
||||
// STRATA_KVA_SKIP=moe: the layer's attention half (GDN or QSA, outputs included) runs in full and only the MoE
|
||||
@@ -5087,13 +5183,22 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
const bool kva_moe_only = kva_layer && kva.skip == KvaConfig::Skip::Moe;
|
||||
const bool kva_state_only = kva_layer && !kva_moe_only;
|
||||
const bool kva_subst = kva_layer && kva.mode == KvaConfig::Mode::Oracle;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool kva_cap_layer = !loop_on && kva.capture && LB == 0 && (LE < 0 || LE == g.n_layers) && l >= kva_split;
|
||||
if (pass == 0 && l == std::max<int64_t>(LB, 1)) { // the PLE rows, read from layer 1 on (a wait for the SSD read of the first chunk lands on "ple")
|
||||
#else
|
||||
const bool kva_cap_layer = kva.capture && LB == 0 && (LE < 0 || LE == g.n_layers) && l >= kva_split;
|
||||
if (l == std::max<int64_t>(LB, 1)) { // the PLE rows, read from layer 1 on (a wait for the SSD read of the first chunk lands on "ple")
|
||||
#endif
|
||||
pt.mark(kPfPle, cs);
|
||||
if (!ple_land()) return false;
|
||||
}
|
||||
// ---- the PLE block at layer 1, token by token (its conv reads the previous tokens' rows)
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (l == 1 && pass == 0 && ple_on && ple_batch) {
|
||||
#else
|
||||
if (l == 1 && ple_on && ple_batch) {
|
||||
#endif
|
||||
// the whole chunk at once, in sub-batches carved from the idle scratch region: the key and value
|
||||
// projections as GEMMs (a token at a time they re-read ~52 MB of BF16 key per token on the IQ
|
||||
// files), the rest with the per-token kernels' arithmetic (native_ple_postops_batch)
|
||||
@@ -5132,7 +5237,11 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
} catch (const std::exception& e) { err = std::string("prefill PLE: ") + e.what(); return false; }
|
||||
}
|
||||
stats_.ms_ple += ms_since(tp);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
} else if (l == 1 && pass == 0 && ple_on) {
|
||||
#else
|
||||
} else if (l == 1 && ple_on) {
|
||||
#endif
|
||||
pt.mark(kPfPle, cs);
|
||||
const auto tp = Clock::now();
|
||||
for (int64_t t = 0; t < T; ++t) {
|
||||
@@ -5154,6 +5263,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
if (!kva_oracle.read(l, m.R, T, p0, D, cs)) { err = kva_oracle.err; return false; }
|
||||
normed = false; // the previous half's norm of R is stale once the input is replaced
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (stp.save_anchor_before) {
|
||||
cudaMemcpyAsync(m.R_anchor, m.R, (size_t) T * D * sizeof(float), cudaMemcpyDeviceToDevice, m.cs);
|
||||
}
|
||||
@@ -5165,13 +5275,18 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
strata::kernels::loop_cross_pass_mixer(m.R, m.R_mprev, m.R, pass - 1, stp.core_step, (int) T, m.cs);
|
||||
normed = false;
|
||||
}
|
||||
#endif
|
||||
for (int half = 0; half < 2; ++half) {
|
||||
if (kva_moe_only && half == 1) {
|
||||
// KVA moe-skip: the attention half wrote its output into R; this layer's MoE half has no consumer
|
||||
// left for an approximated chunk, so it (and its expert transfers) is skipped. R is not normed for
|
||||
// it (the write after the attention half normed it with this layer's FFN weights).
|
||||
normed = false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
consumed = std::max(consumed, seq_start[sidx + 1]);
|
||||
#else
|
||||
consumed = std::max(consumed, seq_start[(size_t) l + 1]);
|
||||
#endif
|
||||
give_back(consumed);
|
||||
break;
|
||||
}
|
||||
@@ -5271,11 +5386,17 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
*wsa = need(v, "ssm_a", err);
|
||||
if (!wqkv || !wg || !wo || !wa || !wb || !wc || !wnm || !wdt || !wsa) return false;
|
||||
pt.mark(kPfGdn, cs);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const int64_t gi = stp.gdn_slot;
|
||||
float* state = ss.gdn_state + (size_t) (gi - ss.gdn_ord0) * gdn_floats;
|
||||
#else
|
||||
float* state = ss.gdn_state + (size_t) (gdn_index - ss.gdn_ord0) * gdn_floats;
|
||||
#endif
|
||||
float* conv = state + (uint64_t) g.ssm_state_size * g.ssm_v_heads * g.ssm_state_size;
|
||||
if (!native_proj(m.gemm, wqkv, m.mixed_h, m.qkv, T, v.name("attn_qkv.weight"), err)) return false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on) strata::kernels::loop_lora_add(m.mixed, (int) N, m.qkv, (int) C, pass, l, strata::kernels::LoopTarget::kGdnQkv, (int) T, m.cs);
|
||||
#endif
|
||||
// KVA: the gate projection feeds only the output normalization, which a state-only layer skips
|
||||
// (STRATA_KVA_GDN_NORM=1 keeps the full recurrence as the A/B control for the state-only one)
|
||||
static const bool kva_gdn_norm = [] {
|
||||
@@ -5284,12 +5405,18 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
}();
|
||||
if ((!kva_state_only || kva_gdn_norm) &&
|
||||
!native_proj(m.gemm, wg, m.mixed_h, m.z, T, v.name("attn_gate.weight"), err)) return false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on && (!kva_state_only || kva_gdn_norm))
|
||||
strata::kernels::loop_lora_add(m.mixed, (int) N, m.z, (int) ZV, pass, l, strata::kernels::LoopTarget::kGdnZ, (int) T, m.cs);
|
||||
#endif
|
||||
if (!bf16_proj(m.gemm, wa, m.mixed_bf, m.ab, T, v.name("ssm_alpha.weight"), err, 2 * HV, m.mixed_bf_lo)) return false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on) strata::kernels::loop_lora_add(m.mixed, (int) N, m.ab, (int) (2 * HV), pass, l, strata::kernels::LoopTarget::kGdnA, (int) T, m.cs);
|
||||
#endif
|
||||
if (!bf16_proj(m.gemm, wb, m.mixed_bf, m.ab + HV, T, v.name("ssm_beta.weight"), err, 2 * HV, m.mixed_bf_lo)) return false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on) strata::kernels::loop_lora_add(m.mixed, (int) N, m.ab + HV, (int) (2 * HV), pass, l, strata::kernels::LoopTarget::kGdnB, (int) T, m.cs);
|
||||
#endif
|
||||
pt.mark(kPfGdnConv, cs); // "gdn" is the projections in; the rest on their own lines
|
||||
gdn_gates(m.ab, (const float*) wdt->data, (const float*) wsa->data, m.gate, m.beta, T, m.cs);
|
||||
const int64_t ld_y = pf_pad() && T >= std::max<int64_t>(pf_switch_min_t(), 64) ? ZV + ZV_PAD : 0;
|
||||
@@ -5309,22 +5436,36 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
gdn_recurrence(state, m.hbuf, m.gate, m.beta, m.z, (const float*) wnm->data, EPS, m.y, m.y_h, T, m.cs,
|
||||
ld_y);
|
||||
pt.mark(kPfGdnOut, cs);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (!kva_state_only) { // KVA: the out projection feeds only the residual write and the MoE half
|
||||
#else
|
||||
if (!kva_state_only) // KVA: the out projection feeds only the residual write and the MoE half
|
||||
#endif
|
||||
if (!native_proj(m.gemm, wo, m.y_h, m.bo, T, v.name("ssm_out.weight"), err, 0, ld_y)) return false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on) strata::kernels::loop_lora_add_f16(m.y_h, (int) (ld_y ? ld_y : ZV), m.bo, (int) N, pass, l, strata::kernels::LoopTarget::kGdnOut, (int) T, m.cs);
|
||||
}
|
||||
#endif
|
||||
++gdn_index;
|
||||
if (kva_state_only) {
|
||||
// the layer consumed no expert: its stream entries must still be released, or the issuer
|
||||
// thread fills the ring and the next fully computed layer waits for a copy that never comes
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
consumed = std::max(consumed, seq_start[sidx + 1]);
|
||||
#else
|
||||
consumed = std::max(consumed, seq_start[(size_t) l + 1]);
|
||||
#endif
|
||||
give_back(consumed);
|
||||
break; // state advanced: no gated residual write, no MoE half of this layer
|
||||
}
|
||||
} else if (half == 0) {
|
||||
// ======================= QSA =======================
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const int64_t qi = stp.qsa_slot;
|
||||
const core::QsaState& st = ss.qsa_states[qi];
|
||||
#else
|
||||
const core::QsaState& st = ss.qsa_states[qsa_index];
|
||||
#endif
|
||||
const core::WeightRef *wq = need(v, "attn_q.weight", err), *wk = need(v, "attn_k.weight", err),
|
||||
*wv = need(v, "attn_v.weight", err), *wo = need(v, "attn_output.weight", err),
|
||||
*wik = need(v, "indexer.k_proj.weight", err),
|
||||
@@ -5335,12 +5476,18 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
if (!wq || !wk || !wv || !wo || !wik || !wiq || !wqn || !wkn || !wiqn || !wikn) return false;
|
||||
pt.mark(kPfQsa, cs);
|
||||
if (!native_proj(m.gemm, wk, m.mixed_h, m.Kc, T, v.name("attn_k.weight"), err)) return false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on) strata::kernels::loop_lora_add(m.mixed, (int) N, m.Kc, kKvW, pass, l, strata::kernels::LoopTarget::kQsaK, (int) T, m.cs);
|
||||
#endif
|
||||
if (!native_proj(m.gemm, wv, m.mixed_h, m.Vc, T, v.name("attn_v.weight"), err)) return false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on) strata::kernels::loop_lora_add(m.mixed, (int) N, m.Vc, kKvW, pass, l, strata::kernels::LoopTarget::kQsaV, (int) T, m.cs);
|
||||
#endif
|
||||
// KVA: the query projections have no consumer when only the KV/indexer state advances
|
||||
if (!kva_state_only && !native_proj(m.gemm, wq, m.mixed_h, m.Qf, T, v.name("attn_q.weight"), err)) return false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on && !kva_state_only) strata::kernels::loop_lora_add(m.mixed, (int) N, m.Qf, kQFull, pass, l, strata::kernels::LoopTarget::kQsaQ, (int) T, m.cs);
|
||||
#endif
|
||||
pt.mark(kPfQsaIdxG, cs); // STRATA_PREFILL_TIMING: "qsa proj" is the k, v and q projections
|
||||
if (!bf16_proj(m.gemm, wik, m.mixed_bf, m.idx_raw, T, v.name("indexer.k_proj.weight"), err, 0, m.mixed_bf_lo)) return false;
|
||||
if (!kva_state_only && !bf16_proj(m.gemm, wiq, m.mixed_bf, m.q_idx, T, v.name("indexer.q_proj.weight"), err, 0, m.mixed_bf_lo)) return false;
|
||||
@@ -5453,7 +5600,11 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
++qsa_index;
|
||||
if (!kv_prefetch_after(l + 1, qsa_index)) return false;
|
||||
// no expert is consumed here: release the stream entries so the issuer cannot stall
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
consumed = std::max(consumed, seq_start[sidx + 1]);
|
||||
#else
|
||||
consumed = std::max(consumed, seq_start[(size_t) l + 1]);
|
||||
#endif
|
||||
give_back(consumed);
|
||||
break;
|
||||
}
|
||||
@@ -5678,7 +5829,9 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
pt.mark(kPfQsa, cs);
|
||||
if (!gated) gate_attn(m.attn, m.Qf, m.attn_h, T, m.cs, ld_a);
|
||||
if (!native_proj(m.gemm, wo, m.attn_h, m.bo, T, v.name("attn_output.weight"), err, 0, ld_a)) return false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on) strata::kernels::loop_lora_add_f16(m.attn_h, (int) (ld_a ? ld_a : kQW), m.bo, (int) N, pass, l, strata::kernels::LoopTarget::kQsaO, (int) T, m.cs);
|
||||
#endif
|
||||
++qsa_index;
|
||||
if (!kv_prefetch_after(l + 1, qsa_index)) return false;
|
||||
} else {
|
||||
@@ -5691,7 +5844,9 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
if (!wr || !wgi || !wsg || !wsu || !wsd) return false;
|
||||
pt.mark(kPfRouter, cs);
|
||||
if (!bf16_proj(m.gemm, wr, m.mixed_bf, m.logits, T, v.name("ffn_gate_inp.weight"), err, 0, m.mixed_bf_lo)) return false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on) strata::kernels::loop_lora_add(m.mixed, (int) N, m.logits, (int) m.g->n_expert, pass, l, strata::kernels::LoopTarget::kMoeGate, (int) T, m.cs);
|
||||
#endif
|
||||
route(m.logits, m.ids, m.w, T, m.g->n_expert, m.cs);
|
||||
// RESEARCH HOOK (141-research, opt-in, never on by default): STRATA_DBG_FEAT=<file> dumps, per MoE layer and chunk,
|
||||
// the routed ids + weights (and for the layers in STRATA_DBG_FEAT_LAYERS the router-input hidden state, bf16);
|
||||
@@ -5756,12 +5911,18 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
// the shared expert and its scalar gate
|
||||
auto shared_expert = [&]() -> bool {
|
||||
if (!native_proj(m.gemm, wsg, m.mixed_h, m.sgate, T, v.name("ffn_gate_shexp.weight"), err)) return false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on) strata::kernels::loop_lora_add(m.mixed, (int) N, m.sgate, kFF, pass, l, strata::kernels::LoopTarget::kShGate, (int) T, m.cs);
|
||||
#endif
|
||||
if (!native_proj(m.gemm, wsu, m.mixed_h, m.sup, T, v.name("ffn_up_shexp.weight"), err)) return false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on) strata::kernels::loop_lora_add(m.mixed, (int) N, m.sup, kFF, pass, l, strata::kernels::LoopTarget::kShUp, (int) T, m.cs);
|
||||
#endif
|
||||
swiglu_pair(m.sgate, m.sup, m.sh_h, T, m.cs);
|
||||
if (!native_proj(m.gemm, wsd, m.sh_h, m.shared, T, v.name("ffn_down_shexp.weight"), err)) return false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on) strata::kernels::loop_lora_add_f16(m.sh_h, kFF, m.shared, (int) N, pass, l, strata::kernels::LoopTarget::kShDown, (int) T, m.cs);
|
||||
#endif
|
||||
if (wgi->kind != core::WeightKind::Bf16InF32) { err = "prefill: shared gate is not BF16"; return false; }
|
||||
m.gemm.bf16(m.mixed_bf, (const uint16_t*) wgi->data, m.sg, T, 1, N);
|
||||
if (m.mixed_bf_lo) m.gemm.bf16(m.mixed_bf_lo, (const uint16_t*) wgi->data, m.sg, T, 1, N, 0, 1.0f);
|
||||
@@ -5890,8 +6051,13 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
// of which at most a third of the ring streamed: the next batch's blobs arrive while one computes.
|
||||
// An expert the routing did not pick has no tiles; its ring slot is given back with its batch.
|
||||
const size_t per = (size_t) std::max(1, m.ring / 3);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
size_t k = seq_start[sidx];
|
||||
const size_t kend = seq_start[sidx + 1];
|
||||
#else
|
||||
size_t k = seq_start[(size_t) l];
|
||||
const size_t kend = seq_start[(size_t) l + 1];
|
||||
#endif
|
||||
// STRATA_PF_BATCH=N (opt-in): experts per launch (the same rows, so the same bits; a launch is one
|
||||
// persistent grid, so fewer launches leave fewer grid tails) - at most kMaxBatch, or up to
|
||||
// kBatchRoom in a WideBatch on the native NT = 256 kernels (STRATA_PF_FUSED_NT=256 or _F16=1) or
|
||||
@@ -6265,7 +6431,11 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
const int32_t* layer_res = m.host_res && m.cache ? m.host_res + (size_t) l * m.g->n_expert : nullptr;
|
||||
if (detail::mmq_resident_sort_eligible(tier_sort || ((sort_all || sort_full) && on_peer.empty()), use_mmq, lay.native,
|
||||
m.cache != nullptr, layer_res, m.g->n_expert, stream_all,
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
!stream_all || seq_start[sidx] == seq_start[sidx + 1], tier_sort)) {
|
||||
#else
|
||||
!stream_all || seq_start[(size_t) l] == seq_start[(size_t) l + 1], tier_sort)) {
|
||||
#endif
|
||||
std::vector<char> layer_pin;
|
||||
if (tier_sort && !stream_all && m.src != nullptr) {
|
||||
layer_pin.assign((size_t) m.g->n_expert, 0);
|
||||
@@ -6661,7 +6831,11 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
lay.native && m.cache != nullptr && m.host_res != nullptr && !wmma_l &&
|
||||
!mmq_plan().direct[(size_t) l] && lay.fmt[(size_t) l].up_off == mmq_gub / 2 &&
|
||||
order_peer.empty() && rows_cpu == 0 && rows_local == T * K && !m.fused_bufs && m.group_cap == 0 &&
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
(!stream_all || seq_start[sidx] == seq_start[sidx + 1]) && !had_gu && !had_d &&
|
||||
#else
|
||||
(!stream_all || seq_start[(size_t) l] == seq_start[(size_t) l + 1]) && !had_gu && !had_d &&
|
||||
#endif
|
||||
!lay.fmt[(size_t) l].tail_off && !mmq::fp4_activations(mmq_gt) &&
|
||||
!mmq::fp4_activations(mmq_dt) && mmq::swiglu_quant_ok(mmq_dt) &&
|
||||
ws::supported(mmq_gt, mmq_dt) && ws::faster(mmq_gt, ws_u) && ws::faster(mmq_dt, ws_u)) {
|
||||
@@ -6864,8 +7038,13 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
const uint8_t* bd = nullptr;
|
||||
int psl = -1;
|
||||
if (on_peer[(size_t) e] == 2) { // from the peer's ring
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
p_release_to(sidx, e, ps);
|
||||
if (P.pk >= P.pseq_start[sidx + 1] || P.pseq[P.pk].e != e) {
|
||||
#else
|
||||
p_release_to(l, e, ps);
|
||||
if (P.pk >= P.pseq_start[(size_t) l + 1] || P.pseq[P.pk].e != e) {
|
||||
#endif
|
||||
err = "prefill: a peer-streamed expert is not next in the peer's ring";
|
||||
cudaSetDevice(prevd);
|
||||
return false;
|
||||
@@ -7106,7 +7285,11 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
int pd = 0;
|
||||
cudaGetDevice(&pd);
|
||||
cudaSetDevice(m.pp->dev);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
p_release_to(sidx, (int32_t) m.g->n_expert, m.pp->s);
|
||||
#else
|
||||
p_release_to(l, (int32_t) m.g->n_expert, m.pp->s);
|
||||
#endif
|
||||
cudaSetDevice(pd);
|
||||
}
|
||||
// STRATA_PREFILL_GROUP_GATHER=0: one gather, one wait and one record per expert.
|
||||
@@ -7645,8 +7828,13 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
} else {
|
||||
// the streamed walk: this layer's entries [k, kend) in id order; an entry the routing did not
|
||||
// pick only gives its slot back
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
size_t k = seq_start[sidx];
|
||||
const size_t kend = seq_start[sidx + 1];
|
||||
#else
|
||||
size_t k = seq_start[(size_t) l];
|
||||
const size_t kend = seq_start[(size_t) l + 1];
|
||||
#endif
|
||||
// STRATA_I8_DEFER=1 (opt-in, the int8 expert groups only): a streamed entry the routing did not pick no longer
|
||||
// launches the open int8 group early. Without it, every such entry while an int8 group holds ring slots runs the
|
||||
// experts gathered so far as a launch of their own (MMQ's flush only gathers, its GEMM still waits for the full
|
||||
@@ -7766,25 +7954,41 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
}
|
||||
}
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_on && strata::kernels::loop_adapter().dual_coda) // strata_dual_coda_loop_v1: the block's LoRA on its HC-mixed input, added to its output
|
||||
strata::kernels::loop_lora_add_gated(m.mixed, (int) N, m.bo, (int) N, pass, l,
|
||||
half == 0 ? strata::kernels::LoopTarget::kAttn : strata::kernels::LoopTarget::kMlp,
|
||||
(int) T, m.cs, m.cd_omega);
|
||||
#endif
|
||||
pt.mark(kPfHcWrite, cs); // the write-back (and the next half's norm) on its own line; it used to be charged to the phase before
|
||||
// ---- the hyper-connection write of this half; F-2: fused with the next half's norm when nothing else
|
||||
// touches R in between (not the stage's last half, not before the PLE block of layer 1, not under a
|
||||
// control vector)
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool next_exists = (half == 0) || (sidx + 1 < loop_plan.size());
|
||||
const int64_t nl = half == 0 ? l : (next_exists ? loop_plan[sidx + 1].layer : LE);
|
||||
#else
|
||||
const int64_t nl = half == 0 ? l : l + 1;
|
||||
#endif
|
||||
const bool steered = half == 1 && strata::kernels::cvec().covers(l);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool loop_mat = half == 1 && loop_on && stp.materialize_after;
|
||||
#endif
|
||||
const float *cv_dir = nullptr, *cv_s = nullptr;
|
||||
const int* cv_on = nullptr;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool cv_fused = steered && !loop_mat && cvec_fuse() && !gr_unfused() && next_exists &&
|
||||
!(nl == 1 && ple_on) &&
|
||||
#else
|
||||
const bool cv_fused = steered && cvec_fuse() && !gr_unfused() && nl < LE && !(nl == 1 && ple_on) &&
|
||||
#endif
|
||||
strata::kernels::cvec().n_embd == N && strata::kernels::cvec().hc == HC &&
|
||||
strata::kernels::cvec_tables(&cv_dir, &cv_s, &cv_on);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool fuse = !gr_unfused() && !loop_mat && next_exists && !(half == 1 && nl == 1 && ple_on) && (!steered || cv_fused);
|
||||
#else
|
||||
const bool fuse = !gr_unfused() && nl < LE && !(half == 1 && nl == 1 && ple_on) && (!steered || cv_fused);
|
||||
#endif
|
||||
const core::WeightRef* wnn = nullptr;
|
||||
if (fuse) {
|
||||
const core::LayerView vn(*m.wt, nl);
|
||||
@@ -7837,6 +8041,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
}
|
||||
if (steered && !cv_fused) // --control-vector-scaled
|
||||
strata::kernels::cvec_apply(m.R, l, T, D, nullptr, 0, nullptr, 0, false, m.cs);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (loop_mat) {
|
||||
if (stp.save_anchor_after) {
|
||||
cudaMemcpyAsync(m.R_anchor, m.R, (size_t) T * D * sizeof(float), cudaMemcpyDeviceToDevice, m.cs);
|
||||
@@ -7851,9 +8056,14 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
cudaMemcpyAsync(m.R_mprev, m.R, (size_t) T * D * sizeof(float), cudaMemcpyDeviceToDevice, m.cs);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if defined(STRATA_HAVE_DFLASH2)
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (half == 1 && tap_rows > 0 && tap_slot[(size_t) l] >= 0 && // DFlash2: this layer's completed output, mean of the streams
|
||||
strata::kernels::loop_tap_pass(stp, la_pf, loop_bypass_)) // (a looped core: its final pass)
|
||||
#else
|
||||
if (half == 1 && tap_rows > 0 && tap_slot[(size_t) l] >= 0) // DFlash2: this layer's completed output, mean of the streams
|
||||
#endif
|
||||
strata::kernels::df2_tap_rows(m.R + (size_t) tap_t0 * D, tap_dev_ + (size_t) tap_slot[(size_t) l] * N, (int64_t) tap_local_.size() * N, N,
|
||||
(int) HC, (int) tap_rows, m.cs);
|
||||
#endif
|
||||
@@ -7875,9 +8085,14 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
std::fprintf(stderr, "strata prefill copies: %lld slot waits, %lld skipped as the same release event\n",
|
||||
(long long) iss_waits, (long long) iss_dedup);
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (cyc_vis_ == nullptr || cyc_vis_->owner_first) stats_.tokens += T;
|
||||
#else
|
||||
stats_.tokens += T;
|
||||
#endif
|
||||
core::progress_at("reading the prompt (batched): finishing the chunk from token", p0);
|
||||
pt.mark(kPfStart, cs);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (cyc_vis_ != nullptr && !cyc_vis_->head) {
|
||||
// a cyclic split's visit before the final one: its rows (and the m_prev it relays) to its hand-off, which
|
||||
// the next visit's stage reads once this returns (run_cyclic); the stage's taps and its checkpoint part
|
||||
@@ -7897,6 +8112,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
}
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
if (next_ != nullptr) {
|
||||
// The current hand-off slot was used two chunks ago.
|
||||
float* h = m.hand[hand_buf_];
|
||||
@@ -7946,6 +8162,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
err = std::string("prefill: the layer split's hand-off: ") + cudaGetErrorString(cudaGetLastError());
|
||||
return false;
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
#if defined(STRATA_HAVE_DFLASH2)
|
||||
// this stage's DFlash2 prompt taps: the landing waits for every tapping stage's rows (a looped adapter only:
|
||||
// the stock split keeps its 0.2.23 behaviour)
|
||||
@@ -7953,6 +8170,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
if (cudaStreamSynchronize(m.cs) != cudaSuccess) { err = std::string("prefill: ") + cudaGetErrorString(cudaGetLastError()); return false; }
|
||||
if (!on_taps(tap_dev_, tap_rows, p0 + tap_t0, err)) return false;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
// Wait only for the DIRECT successor's previous chunk. That successor
|
||||
// may already have forwarded its older chunk to later GPUs.
|
||||
@@ -7978,7 +8196,9 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
next_->hand_pieces_ = hp;
|
||||
next_->single_chunk_ = single_chunk;
|
||||
next_->pipe_chunked_ = pipe_chunked;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
next_->loop_cd_in_thinking_ = chunk_think_before;
|
||||
#endif
|
||||
if (handoff_pipe_worker()) {
|
||||
if (!m.hand_worker) m.hand_worker = std::make_unique<HandWorker>();
|
||||
next_run_ = m.hand_worker->submit(std::packaged_task<bool()>([this, tokens, c0, T, p0] {
|
||||
@@ -8063,10 +8283,15 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
}
|
||||
// Do not drain the successor here. This is the overlap: an intermediate
|
||||
// stage can return while later GPUs are still processing the previous chunk.
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (cyc_vis_ == nullptr || cyc_vis_->owner_first) { // (a cyclic stage's later visits read the same tokens again)
|
||||
ss.ple_prev[0] = prev[0];
|
||||
ss.ple_prev[1] = prev[1];
|
||||
}
|
||||
#else
|
||||
ss.ple_prev[0] = prev[0];
|
||||
ss.ple_prev[1] = prev[1];
|
||||
#endif
|
||||
if (std::getenv("STRATA_DBG_NAN") != nullptr) { // debug: the state the prompt leaves for the token path
|
||||
cudaStreamSynchronize(m.cs);
|
||||
auto bad = [&](const float* d, int64_t n) {
|
||||
@@ -8580,7 +8805,9 @@ bool Prefill::run_multi(const std::vector<Seg>& segs, std::string& err) {
|
||||
for (const Seg& sg : segs) total += sg.n;
|
||||
std::string why;
|
||||
if (hand_in_ != nullptr || !impl_) { err = "prefill: multi-prompt read declined (chunk hand-off in use)"; return false; }
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (cyc_ != nullptr) { err = "prefill: multi-prompt read declined (a cyclic layer split)"; return false; }
|
||||
#endif
|
||||
if (!tap_layers_.empty()) { err = "prefill: multi-prompt read declined (the DFlash2 prompt taps are for one prompt)"; return false; }
|
||||
for (Prefill* q = this; q != nullptr; q = q->next_) q->lm_force_ = true;
|
||||
struct ForceOff { Prefill* h; ~ForceOff() { for (Prefill* q = h; q != nullptr; q = q->next_) q->lm_force_ = false; } } force_off{this};
|
||||
@@ -9753,6 +9980,7 @@ bool Prefill::lm_run(const std::vector<Seg>& segs, int depth, const std::vector<
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
void Prefill::set_cyclic(const std::vector<Prefill*>& paths, const std::vector<int64_t>& stage_begin,
|
||||
const core::ModelGeometry& g) {
|
||||
std::shared_ptr<Cyc> cy;
|
||||
@@ -9920,6 +10148,7 @@ bool Prefill::run_cyclic(const int64_t* tokens, int64_t n, int64_t pos0, std::st
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
bool Prefill::drain_pipeline(std::string& err) {
|
||||
bool ok = true;
|
||||
|
||||
@@ -9987,7 +10216,11 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e
|
||||
#endif
|
||||
// layer-major prefill: STRATA_PREFILL_LAYER_MAJOR=1 / 0 win; unset, it is off everywhere (sm120_series.hpp prefill_layer_major_default: a layer split
|
||||
// reads 32K prompts 37-55% sooner chunk-major, owner's yes 2026-11-02; the single-card small-cache rule sets the variable itself)
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (hand_in_ == nullptr && impl_ && !strata::kernels::loop_adapter().loaded() &&
|
||||
#else
|
||||
if (hand_in_ == nullptr && impl_ &&
|
||||
#endif
|
||||
strata::prefill_layer_major_resolve(lm_env_text(), lm_hip, stage_count(), strata::qwen_defaults_026())) {
|
||||
const bool by_default = !lm_env_explicit();
|
||||
for (Prefill* p = this; p != nullptr; p = p->next_) p->lm_by_default_ = by_default;
|
||||
@@ -10033,7 +10266,9 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e
|
||||
(long long) off_in_row, why.c_str());
|
||||
}
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (cyc_ != nullptr && cyc_stage_ == 0 && !loop_bypass_) return run_cyclic(tokens, n, pos0, err);
|
||||
#endif
|
||||
const bool body_ok = run_impl(tokens, n, pos0, err);
|
||||
|
||||
std::string drain_err;
|
||||
|
||||
@@ -162,7 +162,9 @@
|
||||
#endif
|
||||
#include "strata/program/pipe_probe.hpp"
|
||||
#include "strata/kernels/cvec.hpp"
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
#include "strata/kernels/loop_adapter.hpp"
|
||||
#endif
|
||||
#include "strata/core/progress.hpp"
|
||||
#include "strata/core/stream_watch.hpp"
|
||||
#include "strata/core/gpu_pause_time.hpp"
|
||||
@@ -1208,6 +1210,7 @@ struct Options {
|
||||
int cvec_first = -1, cvec_last = -1; ///< llama.cpp's defaults: 1 .. the last layer
|
||||
int cvec_mode = 1; ///< 0 = project, 1 = add (llama.cpp's default)
|
||||
int cvec_single = -1; ///< --cvec-dir single:L (project mode): layer L's direction everywhere
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
/// Looped Qwen3.8-Flash-Next recurrent core adapter (strata/kernels/loop_adapter.hpp). Opt-in.
|
||||
std::string loop_adapter; ///< --loop-adapter FILE (.safetensors, optional sibling .json)
|
||||
int loop_passes = 0; ///< --loop-passes N (default 2 when --loop-adapter is given)
|
||||
@@ -1216,6 +1219,7 @@ struct Options {
|
||||
bool loop_cd_thinking = false; ///< --loop-cd-thinking (gate LoopCD-Hidden to active <think>...</think> spans)
|
||||
int32_t loop_cd_think_start = 248068; ///< --loop-cd-think-ids START END (<think>, default Qwen3.8-Flash-Next)
|
||||
int32_t loop_cd_think_end = 248069; ///< --loop-cd-think-ids START END (</think>, default Qwen3.8-Flash-Next)
|
||||
#endif
|
||||
};
|
||||
|
||||
void usage() {
|
||||
@@ -1385,12 +1389,14 @@ void usage() {
|
||||
" --control-vector-layer-range A B the layers it follows (inclusive; default 1 .. the last)\n"
|
||||
" --cvec-mode add|project h += s v (default) or h -= s (h.v) v with v unit\n"
|
||||
" --cvec-dir per-layer|single:L each layer's own direction (default) or layer L's everywhere (project)\n"
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
" --loop-adapter FILE Looped Qwen3.8-Flash-Next recurrent adapter (.safetensors + optional sibling .json)\n"
|
||||
" --loop-passes N recurrent core passes T when --loop-adapter is set (default 2; supports 2..8; 2..3 for the Qwen4 dual-coda file)\n"
|
||||
" --loop-coda SET Qwen4-35B-A3B-Loop.safetensors only: universal (Ultra / Balanced / GGUF, default) or compact (the Compact tier)\n"
|
||||
" --loop-cd-omega F optional ratio-preserving LoopCD-Hidden weight after final pass (default 0.0)\n"
|
||||
" --loop-cd-thinking opt-in: gate LoopCD-Hidden to active <think>...</think> spans only\n"
|
||||
" --loop-cd-think-ids START END <think> and </think> token IDs (default 248068 248069; implies --loop-cd-thinking)\n"
|
||||
#endif
|
||||
" --no-token-graph A/B: two graphs per layer (the host launches each) instead of one per token\n"
|
||||
" --no-fused-gr A/B: the six-kernel hyper-connection read and a separate write (native)\n"
|
||||
" --prefill CHUNK batched prompt processing in chunks of CHUNK tokens (needs --native); auto =\n"
|
||||
@@ -4330,6 +4336,7 @@ int main(int argc, char** argv) {
|
||||
else if (d.rfind("single:", 0) == 0) o.cvec_single = std::atoi(d.c_str() + 7);
|
||||
else { std::fprintf(stderr, "--cvec-dir: per-layer or single:L, got '%s'\n", d.c_str()); return 2; }
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
else if (a == "--loop-adapter") o.loop_adapter = next("--loop-adapter");
|
||||
else if (a == "--loop-passes") o.loop_passes = std::atoi(next("--loop-passes"));
|
||||
else if (a == "--loop-coda") o.loop_coda = next("--loop-coda");
|
||||
@@ -4340,6 +4347,7 @@ int main(int argc, char** argv) {
|
||||
o.loop_cd_think_end = (int32_t) std::atoi(next("--loop-cd-think-ids"));
|
||||
o.loop_cd_thinking = true;
|
||||
}
|
||||
#endif
|
||||
else if (a == "--no-spec-split") o.spec_split = false;
|
||||
else if (a == "--eos-ids") {
|
||||
std::string e;
|
||||
@@ -5227,6 +5235,7 @@ int main(int argc, char** argv) {
|
||||
o.lookup_max_t = fix.lookup_max_t;
|
||||
}
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// --loop-adapter: the batched GDN commit is off (each extra pass replays its own states), so a window cannot be wider than
|
||||
// kVerifyMaxT rows: the 12-token lookup default is clamped instead of ending the engine on the first request.
|
||||
if (!o.loop_adapter.empty() && o.lookup_max_t > (int) strata::kernels::kVerifyMaxT) {
|
||||
@@ -5234,6 +5243,7 @@ int main(int argc, char** argv) {
|
||||
o.lookup_max_t, (int) strata::kernels::kVerifyMaxT);
|
||||
o.lookup_max_t = (int) strata::kernels::kVerifyMaxT;
|
||||
}
|
||||
#endif
|
||||
if (o.lookup_max_t > 0 && o.suffix_draft > 0 && o.spec >= 2) {
|
||||
if (o.mtp_max_t == 0) o.mtp_max_t = o.spec;
|
||||
o.spec = std::max(o.spec, std::min(o.lookup_max_t, strata::kernels::kVerifyLongT));
|
||||
@@ -6006,6 +6016,7 @@ int main(int argc, char** argv) {
|
||||
// room; STRATA_STAGE_TRIM=0, STRATA_SPLIT_EARLY_OFF=1 or STRATA_SPILL_OFF=1 keeps the old path.
|
||||
// Dynamic drafter VRAM reserve: Q2_0 S2 stays at kDrafterMib (1000 MiB); larger native packs (e.g. Q4_0 / IQ4_NL:
|
||||
// ~1675 MiB) scale up by their extra expert bytes so layer-split sizing keeps accurate VRAM headroom.
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (!o.loop_adapter.empty()) {
|
||||
if (o.batch > 0) {
|
||||
std::fprintf(stderr, "strata generate: --loop-adapter does not support --batch > 0 yet\n");
|
||||
@@ -6023,6 +6034,7 @@ int main(int argc, char** argv) {
|
||||
std::fprintf(stderr, "strata generate: loop adapter loaded (%s) from %s\n", loop_sum.c_str(),
|
||||
o.loop_adapter.c_str());
|
||||
}
|
||||
#endif
|
||||
const int64_t mtp_drafter_mib =
|
||||
!o.mtp.empty() ? strata::core::MtpDrafter::reserve_mib(o.mtp, strata::program::stage_reserve::kDrafterMib)
|
||||
: strata::program::stage_reserve::kDrafterMib;
|
||||
@@ -6103,11 +6115,15 @@ int main(int argc, char** argv) {
|
||||
std::vector<int64_t> at;
|
||||
std::string ks;
|
||||
int64_t run = 0;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
for (int i = 0; i + 1 < nd; ++i) {
|
||||
run += share[(size_t) i];
|
||||
at.push_back(run);
|
||||
ks += (ks.empty() ? "" : ",") + std::to_string(run);
|
||||
}
|
||||
#else
|
||||
for (int i = 0; i + 1 < nd; ++i) { run += share[(size_t) i]; at.push_back(run); ks += (ks.empty() ? "" : ",") + std::to_string(run); }
|
||||
#endif
|
||||
std::fprintf(stderr, "strata generate: short of GPU memory: CUDA%d has %.2f GiB free and a layer split card needs about %.2f GiB beside a full copy of the dense weights "
|
||||
"(%.2f GiB): the split is chosen from what each card has free (--layer-split %s) and every card loads only the dense weights of its own layers "
|
||||
"(STRATA_STAGE_TRIM=0 keeps the copies)\n",
|
||||
@@ -6774,7 +6790,9 @@ int main(int argc, char** argv) {
|
||||
: batch_blocks_pw ? "not with --batch slots (STRATA_PW_WITH_BATCH=1 keeps it for the solo path)"
|
||||
: o.peer_device >= 1 ? "not with --peer-device"
|
||||
: helpers ? "not with the helper caches (--expert-cache-device1..3, --remote-expert-opt)"
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
: strata::kernels::loop_adapter().loaded() ? "not with --loop-adapter (a cut inside the recurrent core visits the stages in turn)"
|
||||
#endif
|
||||
: nullptr;
|
||||
if (off != nullptr) {
|
||||
std::fprintf(stderr, "strata generate: --pipeline-windows %d is off: %s\n", o.pipeline_windows, off);
|
||||
@@ -6875,10 +6893,12 @@ int main(int argc, char** argv) {
|
||||
std::fprintf(stderr, "strata generate: layer split, CUDA%d: %s\n", st.dev, err.c_str());
|
||||
return 1;
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (strata::kernels::loop_adapter().loaded() && !strata::kernels::loop_adapter_replicate(err)) {
|
||||
std::fprintf(stderr, "strata generate: layer split, CUDA%d: %s\n", st.dev, err.c_str());
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
// --vision: this device's image-position table (the identity until a picture request), read by every rope
|
||||
// kernel its stage runs - set before any of its graphs is captured
|
||||
if (o.vision) {
|
||||
@@ -7242,6 +7262,7 @@ int main(int argc, char** argv) {
|
||||
return native_pack ? strata::core::ExpertCache::slot_stride((int64_t) nib::slot_bytes(l), slot_guard)
|
||||
: (int64_t) lay.max_blob;
|
||||
};
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// --loop-adapter: a window runs the recurrent core's layers once per pass, so a stage's work is its layers plus
|
||||
// (passes - 1) x its core layers, and a core layer's misses are paid on every pass. A cut inside the core is a
|
||||
// cyclic split (the window visits the stages in turn); two equal cards balance at the core's middle.
|
||||
@@ -7254,6 +7275,7 @@ int main(int argc, char** argv) {
|
||||
const int64_t in_core = std::max<int64_t>(0, std::min<int64_t>(le, lad.core_end) - std::max<int64_t>(lb, lad.core_start));
|
||||
return (double) (le - lb) + (double) (lad.num_passes - 1) * (double) in_core;
|
||||
};
|
||||
#endif
|
||||
// the predicted window time (ms) of a placement, and the routed mass its caches hold
|
||||
std::vector<int64_t> held_cnt((size_t) ns, 0); // per-stage fill count, for the startability gate
|
||||
const int64_t kAutoLendPct = [] {
|
||||
@@ -7282,12 +7304,20 @@ int main(int argc, char** argv) {
|
||||
const int64_t l = profile[r].first;
|
||||
int st = 0;
|
||||
while (st + 1 < ns && l >= at[(size_t) st]) ++st;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
mass_i[(size_t) st] += mass[r] * loop_w(l);
|
||||
#else
|
||||
mass_i[(size_t) st] += mass[r];
|
||||
#endif
|
||||
if (full[(size_t) st]) continue;
|
||||
if (used[(size_t) st] + cost(l) > capr[(size_t) st]) { full[(size_t) st] = true; continue; } // as the fill
|
||||
used[(size_t) st] += cost(l);
|
||||
held_mass += mass[r];
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
held_i[(size_t) st] += mass[r] * loop_w(l);
|
||||
#else
|
||||
held_i[(size_t) st] += mass[r];
|
||||
#endif
|
||||
++held;
|
||||
++held_cnt[(size_t) st];
|
||||
}
|
||||
@@ -7299,7 +7329,11 @@ int main(int argc, char** argv) {
|
||||
std::max(total_mass, 1e-9);
|
||||
for (int i = 0; i < ns; ++i) {
|
||||
const int64_t lb = i == 0 ? 0 : at[(size_t) i - 1], le = i + 1 < ns ? at[(size_t) i] : g.n_layers;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
ms += work(lb, le) * layer_ms[(size_t) i];
|
||||
#else
|
||||
ms += (double) (le - lb) * layer_ms[(size_t) i];
|
||||
#endif
|
||||
}
|
||||
return ms;
|
||||
};
|
||||
@@ -7318,7 +7352,11 @@ int main(int argc, char** argv) {
|
||||
double m = 0;
|
||||
for (int i = 0; i < ns; ++i) {
|
||||
const int64_t lb = i == 0 ? 0 : cuts[(size_t) i - 1], le = i + 1 < ns ? cuts[(size_t) i] : g.n_layers;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
m = std::max(m, work(lb, le) * layer_ms[(size_t) i]);
|
||||
#else
|
||||
m = std::max(m, (double) (le - lb) * layer_ms[(size_t) i]);
|
||||
#endif
|
||||
}
|
||||
return m;
|
||||
};
|
||||
@@ -7474,6 +7512,7 @@ int main(int argc, char** argv) {
|
||||
double acc = 0;
|
||||
for (int i = 0; i + 1 < ns; ++i) {
|
||||
acc += 1.0 / layer_ms[(size_t) i];
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
int64_t k = (int64_t) std::llround(acc / total * (double) L);
|
||||
if (lad.loaded()) { // the cut where the stages before it hold their share of the window's work
|
||||
const double want = acc / total * work(0, L);
|
||||
@@ -7481,6 +7520,10 @@ int main(int argc, char** argv) {
|
||||
while (k < L && work(0, k) + 0.5 * loop_w(k) <= want) ++k;
|
||||
}
|
||||
at[(size_t) i] = std::clamp<int64_t>(k, i == 0 ? 2 : at[(size_t) i - 1] + 1, L - (ns - 1 - i));
|
||||
#else
|
||||
at[(size_t) i] = std::clamp<int64_t>((int64_t) std::llround(acc / total * (double) L),
|
||||
i == 0 ? 2 : at[(size_t) i - 1] + 1, L - (ns - 1 - i));
|
||||
#endif
|
||||
}
|
||||
consider();
|
||||
};
|
||||
@@ -7519,29 +7562,47 @@ int main(int argc, char** argv) {
|
||||
std::vector<double> held_mass_at((size_t) ns * (size_t) L * stride, -1.0);
|
||||
std::vector<int64_t> held_at(held_mass_at.size(), 0);
|
||||
std::vector<int64_t> used_at(held_mass_at.size(), 0); // the fill's byte total, for the startability gate
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
std::vector<double> miss_w_at(lad.loaded() ? held_mass_at.size() : 0, 0.0); // --loop-adapter: pass-weighted missed mass
|
||||
#endif
|
||||
auto range_hold = [&](int i, int64_t lb, int64_t le) -> size_t {
|
||||
const size_t key = ((size_t) i * (size_t) L + (size_t) lb) * stride + (size_t) le;
|
||||
if (held_mass_at[key] < 0.0) {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// one stage's own carve, exactly as `predict` prices it (--loop-adapter: with predict's trim credit too;
|
||||
// the stock search keeps its own rule)
|
||||
const int64_t room = miss_w_at.empty() ? stage_left(i, lb, le)
|
||||
: stage_left(i, lb, le) + strata::program::split_rules::trim_credit(i, trim_frees(lb, le));
|
||||
double hm = 0, mw = 0, hw = 0;
|
||||
#else
|
||||
// one stage's own carve, exactly as `predict` prices it
|
||||
const int64_t room = stage_left(i, lb, le);
|
||||
double hm = 0;
|
||||
#endif
|
||||
int64_t used = 0, cnt = 0;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
bool stop = false;
|
||||
#endif
|
||||
for (size_t r = 0; r < profile.size(); ++r) {
|
||||
const int64_t l = profile[r].first;
|
||||
if (l < lb || l >= le) continue;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
mw += mass[r] * loop_w(l);
|
||||
if (stop) continue;
|
||||
if (used + cost(l) > room) { stop = true; if (miss_w_at.empty()) break; continue; } // the fill stops exactly where predict's does
|
||||
#else
|
||||
if (used + cost(l) > room) break; // the fill stops exactly where predict's does
|
||||
#endif
|
||||
used += cost(l);
|
||||
hm += mass[r];
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
hw += mass[r] * loop_w(l);
|
||||
#endif
|
||||
++cnt;
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (!miss_w_at.empty()) miss_w_at[key] = mw - hw;
|
||||
#endif
|
||||
held_mass_at[key] = hm;
|
||||
held_at[key] = cnt;
|
||||
used_at[key] = used;
|
||||
@@ -7555,6 +7616,7 @@ int main(int argc, char** argv) {
|
||||
const size_t a = range_hold(0, 0, k1), b = range_hold(1, k1, k2);
|
||||
const size_t c = range_hold(2, k2, k3), d = range_hold(3, k3, L);
|
||||
const double hm = held_mass_at[a] + held_mass_at[b] + held_mass_at[c] + held_mass_at[d];
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const double ms = miss_w_at.empty()
|
||||
? miss_ms * (1.0 - hm / denom) +
|
||||
(double) k1 * layer_ms[0] + (double) (k2 - k1) * layer_ms[1] +
|
||||
@@ -7563,6 +7625,11 @@ int main(int argc, char** argv) {
|
||||
link_scale[2] * miss_w_at[c] + link_scale[3] * miss_w_at[d]) / denom +
|
||||
work(0, k1) * layer_ms[0] + work(k1, k2) * layer_ms[1] +
|
||||
work(k2, k3) * layer_ms[2] + work(k3, L) * layer_ms[3];
|
||||
#else
|
||||
const double ms = miss_ms * (1.0 - hm / denom) +
|
||||
(double) k1 * layer_ms[0] + (double) (k2 - k1) * layer_ms[1] +
|
||||
(double) (k3 - k2) * layer_ms[2] + (double) (L - k3) * layer_ms[3];
|
||||
#endif
|
||||
if (min_stage_layers > 1 &&
|
||||
strata::program::split_rules::smallest_stage({k1, k2, k3}, L) < min_stage_layers) continue;
|
||||
if (!stage_floors.empty() && strata::program::split_rules::below_floors({k1, k2, k3}, L, stage_floors)) continue;
|
||||
@@ -13219,6 +13286,7 @@ int main(int argc, char** argv) {
|
||||
strata::prefill::Prefill* room_path = nullptr;
|
||||
auto init_prompt_paths = [&]() -> int { // 0: ready; 1: failed (err set); 2: failed with "do not fit"
|
||||
room_path = nullptr;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (multi_gpu && !stages.empty()) { // --loop-adapter, a cut inside the core: the paths read a chunk's visits in turn
|
||||
std::vector<int64_t> sbeg{0};
|
||||
sbeg.insert(sbeg.end(), split_at.begin(), split_at.end());
|
||||
@@ -13233,6 +13301,7 @@ int main(int argc, char** argv) {
|
||||
strata::prefill::Prefill::set_cyclic(paths, sbeg, g);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
for (size_t i = 0; i < stages.size(); ++i) {
|
||||
GpuStage& st = *stages[i];
|
||||
st.sp.set_stage(st.lb, i + 1 < stages.size() ? st.le : -1, i + 1 < stages.size() ? &stages[i + 1]->sp : nullptr);
|
||||
@@ -13477,7 +13546,9 @@ int main(int argc, char** argv) {
|
||||
// (--split-device 0: the second stage on this GPU, sharing its weights, session and cache - the A/B.)
|
||||
strata::core::Verifier ver_same;
|
||||
SplitDrive split_drive;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
std::vector<strata::core::Verifier::CycHop> cyc_hops; // a cyclic split's itinerary (every stage's verifier points at it)
|
||||
#endif
|
||||
auto stage_ver = [&](int st) -> strata::core::Verifier& {
|
||||
return st == 0 ? ver : split_same ? ver_same : stages[(size_t) st - 1]->ver;
|
||||
};
|
||||
@@ -13584,6 +13655,7 @@ int main(int argc, char** argv) {
|
||||
const char* v = std::getenv("STRATA_SPLIT_OVERLAP");
|
||||
return v != nullptr && std::atoi(v) != 0;
|
||||
}();
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// --loop-adapter with a cut inside the recurrent core: the window visits the stages in turn (a cyclic split)
|
||||
std::vector<int64_t> stage_begin{0};
|
||||
stage_begin.insert(stage_begin.end(), split_at.begin(), split_at.end());
|
||||
@@ -13592,10 +13664,15 @@ int main(int argc, char** argv) {
|
||||
std::fprintf(stderr, "strata serve: --loop-adapter: a cut inside the recurrent core needs each stage on its own GPU (not --split-device 0)\n");
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
// --pipeline-windows (#859) runs its own overlapped loop with its own hand-offs: the two do not mix
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool overlap = overlap_asked && !split_same && o.pipeline_windows == 0 && !cyc_split;
|
||||
if (overlap_asked && cyc_split)
|
||||
std::fprintf(stderr, "strata serve: STRATA_SPLIT_OVERLAP is off on a cyclic layer split (--loop-adapter)\n");
|
||||
#else
|
||||
const bool overlap = overlap_asked && !split_same && o.pipeline_windows == 0;
|
||||
#endif
|
||||
if (overlap_asked && o.pipeline_windows > 0)
|
||||
std::fprintf(stderr, "strata serve: STRATA_SPLIT_OVERLAP is off beside --pipeline-windows %d "
|
||||
"(that loop overlaps the stages itself)\n", o.pipeline_windows);
|
||||
@@ -13629,6 +13706,7 @@ int main(int argc, char** argv) {
|
||||
split_drive.cache_slot_off[st] = drive.d.cache_slot_off;
|
||||
split_drive.pcie_num[st] = pcie_num_of(o.pcie_frac);
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
size_t cyc_visits = 0;
|
||||
if (cyc_split) {
|
||||
// one hand-in per stage, read by each of its visits but the first (the visits run one after another);
|
||||
@@ -13658,6 +13736,7 @@ int main(int argc, char** argv) {
|
||||
stage_ver(st).set_cyclic(std::move(per[(size_t) st]), std::move(outs[(size_t) st]), cyc_in[(size_t) st], &cyc_hops);
|
||||
cyc_visits = itin.size();
|
||||
}
|
||||
#endif
|
||||
for (int st = 1; st < n_stages; ++st) {
|
||||
bool ok_s = false;
|
||||
if (split_same) {
|
||||
@@ -13695,10 +13774,12 @@ int main(int argc, char** argv) {
|
||||
" (CUDA" + std::to_string(split_same ? 0 : stages[(size_t) st - 1]->dev) + ")";
|
||||
std::fprintf(stderr, "strata serve: layer split: layers %s, one hand-off per window%s\n", plan_s.c_str(),
|
||||
overlap ? " (overlapped: STRATA_SPLIT_OVERLAP=1)" : "");
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (cyc_split)
|
||||
std::fprintf(stderr, "strata serve: layer split: the cut is inside the looped core [%d,%d): each window visits the "
|
||||
"stages %zu times in turn (T=%d)\n", strata::kernels::loop_adapter().core_start,
|
||||
strata::kernels::loop_adapter().core_end, cyc_visits, strata::kernels::loop_adapter().num_passes);
|
||||
#endif
|
||||
}
|
||||
// --pipeline-windows: the odd windows' verifiers and their hand-off (initialized before `ver`, which stays the
|
||||
// watchdog's verifier), and the drafter's own row buffer, which either parity's rows are copied into
|
||||
@@ -14127,9 +14208,11 @@ int main(int argc, char** argv) {
|
||||
// checkpoints taken inside it
|
||||
ConvCheckpoint::PromptPlan seg_plan;
|
||||
bool cvec_cached = true; // the control vector's state the live session and the checkpoints were read with
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
uint64_t loop_key_cached = strata::kernels::loop_cache_key(
|
||||
strata::kernels::loop_adapter().num_passes, strata::kernels::loop_adapter().cd_omega,
|
||||
strata::kernels::loop_adapter().cd_thinking); // looped execution mode the live session and checkpoints were read with
|
||||
#endif
|
||||
// without --mtp the conversation cache stays on: a parked image carries the draft layer's K/V only when there is one
|
||||
// (the snapshots accept a null draft)
|
||||
// #3493: no --conversation-cache-mib given: size the cache for agent use from the RAM that is free now that the model is
|
||||
@@ -14312,12 +14395,14 @@ int main(int argc, char** argv) {
|
||||
{"keep_canonical", o.keep_canonical}, {"no_fused_gr", o.no_fused_gr},
|
||||
{"no_fast_attn", o.no_fast_attn}, {"no_fused_gdn", o.no_fused_gdn},
|
||||
{"no_fast_select", o.no_fast_select}, {"vision", o.vision}};
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (strata::kernels::loop_adapter().loaded()) {
|
||||
const auto& lad = strata::kernels::loop_adapter();
|
||||
c.switches.push_back({"loop_passes", lad.num_passes});
|
||||
c.switches.push_back({"loop_core_start", lad.core_start});
|
||||
c.switches.push_back({"loop_core_end", lad.core_end});
|
||||
}
|
||||
#endif
|
||||
config_fp = strata::core::session_config_fingerprint(c);
|
||||
}
|
||||
id.model = *model_fp;
|
||||
@@ -14484,7 +14569,9 @@ int main(int argc, char** argv) {
|
||||
const std::vector<ImgKey>& imgs;
|
||||
std::vector<ConvCheckpoint>& checks; ///< moved apart into stage parts for the capture, put back after
|
||||
bool cvec;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
uint64_t loop_key;
|
||||
#endif
|
||||
strata::core::SessionState& session; ///< stage 0's
|
||||
std::vector<strata::core::SessionState*> stage_sessions; ///< the later stages', in stage order (stages[k]->dev)
|
||||
const strata::core::QsaState* draft0; ///< the draft layer's K/V saved with stage 0's image (or none)
|
||||
@@ -14569,14 +14656,22 @@ int main(int argc, char** argv) {
|
||||
// nothing the outgoing chain does not, apart from the tail this conversation rewrote)
|
||||
{
|
||||
std::vector<size_t> gone;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const size_t dropped = conversations.drop_superseded(src.ids, src.imgs, src.checks, src.cvec, &gone, src.loop_key);
|
||||
#else
|
||||
const size_t dropped = conversations.drop_superseded(src.ids, src.imgs, src.checks, src.cvec, &gone);
|
||||
#endif
|
||||
if (dropped)
|
||||
std::fprintf(stderr, "strata serve: conversation cache: dropped %zu superseded cop%s of this "
|
||||
"conversation; parked=%zu\n", dropped, dropped == 1 ? "y" : "ies", conversations.size());
|
||||
// a longer snapshot of the same conversation makes the older ones useless, in RAM and on the SSD tier alike
|
||||
// (never spilled, and a spilled one is deleted with its index file)
|
||||
if (spill.enabled()) {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const auto on_ssd = spill.drop_superseded(src.ids, src.imgs, src.cvec, src.loop_key);
|
||||
#else
|
||||
const auto on_ssd = spill.drop_superseded(src.ids, src.imgs, src.cvec);
|
||||
#endif
|
||||
gone.insert(gone.end(), on_ssd.begin(), on_ssd.end());
|
||||
}
|
||||
for (const size_t n : gone)
|
||||
@@ -14598,11 +14693,19 @@ int main(int argc, char** argv) {
|
||||
strata::core::ConversationCheckpointSplit& cs; std::vector<ConvCheckpoint>& checks; bool on;
|
||||
~MergeBack() { if (on && !strata::core::conversation_checkpoints_merge(std::move(cs), checks)) checks.clear(); }
|
||||
} merge_back{cs, checks, n_st > 0};
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const strata::core::ConversationView view{src.ids, src.imgs, n_st > 0 ? cs.stage0 : checks, src.cvec, src.loop_key};
|
||||
#else
|
||||
const strata::core::ConversationView view{src.ids, src.imgs, n_st > 0 ? cs.stage0 : checks, src.cvec};
|
||||
#endif
|
||||
const int64_t retained_n = src.with_reuse ? conversations.retained_tokens() : 0;
|
||||
// a batch slot's image carries no retained K/V (it describes the main session's conversation), and the
|
||||
// main session's stays retained for its own park
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
auto reuse = src.with_reuse ? conversations.take_reuse_for(src.ids, src.loop_key) // never another conversation's K/V
|
||||
#else
|
||||
auto reuse = src.with_reuse ? conversations.take_reuse_for(src.ids) // never another conversation's K/V
|
||||
#endif
|
||||
: strata::core::ConversationKvReuse{};
|
||||
if (retained_n > 0 && reuse.unchanged_tokens < retained_n)
|
||||
std::fprintf(stderr, "strata serve: conversation cache: retained K/V reused for %lld of its %lld tokens "
|
||||
@@ -14616,7 +14719,11 @@ int main(int argc, char** argv) {
|
||||
for (size_t k = 0; k < n_st; ++k) {
|
||||
auto& st = stages[k];
|
||||
const strata::core::OnDevice on(st->dev);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const strata::core::ConversationView view_k{src.ids, src.imgs, cs.parts[k], src.cvec, src.loop_key};
|
||||
#else
|
||||
const strata::core::ConversationView view_k{src.ids, src.imgs, cs.parts[k], src.cvec};
|
||||
#endif
|
||||
size_t b = 0;
|
||||
if (!strata::core::conversation_snapshot_bytes(view_k, *src.stage_sessions[k], g, draft_of(k), b, err)) {
|
||||
std::fprintf(stderr, "strata serve: conversation cache: skip parking (stage CUDA%d: %s)\n",
|
||||
@@ -14767,7 +14874,11 @@ int main(int argc, char** argv) {
|
||||
auto& st = stages[k];
|
||||
const strata::core::OnDevice on(st->dev);
|
||||
if (cudaDeviceSynchronize() != cudaSuccess) { err = "stage sync failed"; return false; }
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const strata::core::ConversationView view_k{src.ids, src.imgs, cs.parts[k], src.cvec, src.loop_key};
|
||||
#else
|
||||
const strata::core::ConversationView view_k{src.ids, src.imgs, cs.parts[k], src.cvec};
|
||||
#endif
|
||||
strata::core::SavedConversation part;
|
||||
if (!strata::core::conversation_snapshot_save(part, view_k, *src.stage_sessions[k], g, draft_of(k), err,
|
||||
std::move(stage_reuse[k]), &reused_bytes))
|
||||
@@ -14812,7 +14923,11 @@ int main(int argc, char** argv) {
|
||||
auto park_current_body = [&](size_t held) -> bool {
|
||||
if (!conversations.enabled() || !live_ok || live.empty()) return true;
|
||||
const size_t n_st = stages.size();
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
ParkSource src{live, live_imgs, checks, cvec_cached, loop_key_cached, ss, {},
|
||||
#else
|
||||
ParkSource src{live, live_imgs, checks, cvec_cached, ss, {},
|
||||
#endif
|
||||
(n_st > 0 && !mtp_first) || !use_mtp ? nullptr : &mtp.kv_state(),
|
||||
use_mtp && !mtp_first && n_st > 0 ? &mtp.kv_state() : nullptr, true, ""};
|
||||
for (auto& st : stages) src.stage_sessions.push_back(&st->ss);
|
||||
@@ -15128,8 +15243,12 @@ int main(int argc, char** argv) {
|
||||
part_at.erase(part_at.begin(), part_at.upper_bound(done));
|
||||
}
|
||||
bool complete = parts.size() == stages.size() + 1;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// a saved part holds the prefix's ids (a stage of QSA layers only has no GDN bytes); a failed copy left it empty
|
||||
for (const ConvCheckpoint& k : parts) complete = complete && (int64_t) k.ids.size() == done;
|
||||
#else
|
||||
for (const ConvCheckpoint& k : parts) complete = complete && !k.gdn.empty();
|
||||
#endif
|
||||
saved = !complete || checkpoint_at(done, &parts); // an incomplete set: no checkpoint here
|
||||
} else {
|
||||
saved = checkpoint_at(done, nullptr, tail_checkpoint && !periodic_checkpoint);
|
||||
@@ -16490,6 +16609,7 @@ int main(int argc, char** argv) {
|
||||
slots_all += remote_experts[(size_t) r].resident();
|
||||
mib_all += (int64_t) (remote_experts[(size_t) r].gib() * 1024.0);
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
std::string loop_info_s;
|
||||
if (strata::kernels::loop_adapter().loaded()) {
|
||||
const auto& lad = strata::kernels::loop_adapter();
|
||||
@@ -16498,6 +16618,7 @@ int main(int argc, char** argv) {
|
||||
lad.num_passes, (double) lad.cd_omega, lad.cd_thinking ? 1 : 0);
|
||||
loop_info_s = lbuf;
|
||||
}
|
||||
#endif
|
||||
// (#5012: experts = every routed (layer, expert) pair, so the Monitor shows "slots / experts" as a share)
|
||||
std::printf("INFO logit_bias=1 context=%lld kv=%s kv_resident=%lld expert_slots=%lld experts=%lld expert_cache_mib=%lld "
|
||||
"expert_slots_primary=%lld expert_cache_primary_mib=%lld spec=%d "
|
||||
@@ -16505,7 +16626,11 @@ int main(int argc, char** argv) {
|
||||
"spec_min_p=%.2f conversation_cache_mib=%lld conversation_cache_slots=%d "
|
||||
"conversation_cache_min_free_mib=%lld conversation_cache_min_tokens=%lld "
|
||||
"tail_role_token=%lld vram_elastic=%d "
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
"prefill_cap=%lld prefill_stages=%d stream_all_min=%lld prefill_pipe=%g%s%s engine=" STRATA_VERSION "\n",
|
||||
#else
|
||||
"prefill_cap=%lld prefill_stages=%d stream_all_min=%lld prefill_pipe=%g%s engine=" STRATA_VERSION "\n",
|
||||
#endif
|
||||
(long long) o.max_context, o.kv.c_str(),
|
||||
(long long) (g.n_qsa_layers() > 0 && ss.qsa_states[ss.qsa_primary()].kv_mode == 1
|
||||
? ss.qsa_states[ss.qsa_primary()].n_slots * 4 : 0),
|
||||
@@ -16526,8 +16651,12 @@ int main(int argc, char** argv) {
|
||||
o.batch > 0 ? (" batch_slots=" + std::to_string(o.batch) +
|
||||
" slot_cache=" + std::to_string(o.prompt_cache > 0 ? 1 : 0) +
|
||||
" batch_groups=" + std::to_string(o.batch_groups) +
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
" slot_read=" + std::to_string(slot_read_cap ? 1 : 0)).c_str() : "",
|
||||
loop_info_s.c_str());
|
||||
#else
|
||||
" slot_read=" + std::to_string(slot_read_cap ? 1 : 0)).c_str() : "");
|
||||
#endif
|
||||
if (strata::core::manifest::enabled()) { // STRATA_MANIFEST: engine choice and budgets (a diagnostic; off by default)
|
||||
auto& M = strata::core::manifest::instance();
|
||||
M.engine("version", STRATA_VERSION);
|
||||
@@ -16690,7 +16819,9 @@ int main(int argc, char** argv) {
|
||||
} else {
|
||||
strata::core::session_zero(ss, g, nullptr, main_cs);
|
||||
cudaStreamSynchronize(main_stream);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
ver.set_loop_cd_in_thinking(false);
|
||||
#endif
|
||||
}
|
||||
drive.d.layers = 0;
|
||||
drive.d.experts = 0;
|
||||
@@ -17394,7 +17525,11 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
const size_t n_st = stages.size();
|
||||
static const std::vector<ImgKey> no_imgs;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
ParkSource src{sl.ids, no_imgs, sl.checks, sl.cvec, 0, *bslot_ss[0][(size_t) b], {},
|
||||
#else
|
||||
ParkSource src{sl.ids, no_imgs, sl.checks, sl.cvec, *bslot_ss[0][(size_t) b], {},
|
||||
#endif
|
||||
(n_st > 0 && !mtp_first) || !batch_mtp || batch_df2 ? nullptr : &slot_mtp[(size_t) b]->kv_state(),
|
||||
batch_mtp && !batch_df2 && !mtp_first && n_st > 0 ? &slot_mtp[(size_t) b]->kv_state() : nullptr, false,
|
||||
" from slot " + std::to_string(b)};
|
||||
@@ -18995,7 +19130,11 @@ int main(int argc, char** argv) {
|
||||
continue;
|
||||
}
|
||||
kept = disk_checks.size();
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const strata::core::ConversationView view{live, live_imgs, disk_checks, cvec_cached, loop_key_cached};
|
||||
#else
|
||||
const strata::core::ConversationView view{live, live_imgs, disk_checks, cvec_cached};
|
||||
#endif
|
||||
strata::core::SavedConversation meta;
|
||||
std::vector<strata::core::SessionKvSource> sources;
|
||||
// the live running state comes off the device in one synchronous copy
|
||||
@@ -19093,7 +19232,9 @@ int main(int argc, char** argv) {
|
||||
checks = std::move(image.checkpoints);
|
||||
for (const ConvCheckpoint& c : checks) check_clock = std::max(check_clock, c.used);
|
||||
cvec_cached = image.cvec;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
loop_key_cached = image.loop_key;
|
||||
#endif
|
||||
live_ok = true;
|
||||
if (nan_flush) { // #879: the file replaced the poisoned session; the parked ones go (no slots here)
|
||||
nan_flush = false;
|
||||
@@ -19138,6 +19279,7 @@ int main(int argc, char** argv) {
|
||||
std::string req_bias_text;
|
||||
bool req_bias_present = false;
|
||||
int req_cvec = 1; // cvec=0|1: a loaded control vector for this request (on when absent)
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// Looped adapter per-request overrides (absent = engine start-up defaults):
|
||||
// loop_passes=N: 1 (stock 48-layer bypass) or the loaded --loop-passes N
|
||||
// loop_cd_omega=F: per-request LoopCD-Hidden extrapolation weight (0.0 disables)
|
||||
@@ -19147,6 +19289,7 @@ int main(int argc, char** argv) {
|
||||
bool req_loop_passes_set = false;
|
||||
float req_loop_cd_omega = req_la.loaded() ? req_la.cd_omega : 0.0f;
|
||||
bool req_loop_cd_thinking = req_la.loaded() ? req_la.cd_thinking : false;
|
||||
#endif
|
||||
// logprobs=K (0..20): after each "T id" an "LP logprob id:logprob ..." line.
|
||||
// -1 (absent) = no LP lines, so a server that never asks sees today's output.
|
||||
int req_logprobs = -1;
|
||||
@@ -19200,9 +19343,11 @@ int main(int argc, char** argv) {
|
||||
if (key == "logit_bias") { req_bias_text = tok.substr(eq + 1); req_bias_present = true; }
|
||||
else if (key == "pen_from") req_pen_from = std::atoll(tok.c_str() + eq + 1);
|
||||
else if (key == "cvec") req_cvec = std::atoi(tok.c_str() + eq + 1);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
else if (key == "loop_passes") { req_loop_passes = std::atoi(tok.c_str() + eq + 1); req_loop_passes_set = true; }
|
||||
else if (key == "loop_cd_omega") req_loop_cd_omega = fv;
|
||||
else if (key == "loop_cd_thinking") req_loop_cd_thinking = std::atoi(tok.c_str() + eq + 1) != 0;
|
||||
#endif
|
||||
else if (key == "ckpt") req_ckpt = std::atoi(tok.c_str() + eq + 1) != 0;
|
||||
else if (key == "prefill") req_prefill_only = std::atoi(tok.c_str() + eq + 1) != 0;
|
||||
else if (key == "pin") req_pin = std::max<long long>(0, std::atoll(tok.c_str() + eq + 1));
|
||||
@@ -19258,6 +19403,7 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
}
|
||||
strata::prefill::Prefill::set_pipe_request(req_pipe); // every request sets it: none carries over
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (req_loop_passes_set) {
|
||||
if (!req_la.loaded()) {
|
||||
if (req_loop_passes != 1) {
|
||||
@@ -19278,6 +19424,7 @@ int main(int argc, char** argv) {
|
||||
if (!req_la.loaded() || req_loop_passes <= 1) {
|
||||
req_loop_cd_omega = 0.0f;
|
||||
}
|
||||
#endif
|
||||
bool req_df2 = use_df2; // --draft auto: this request's drafter, decided once the prompt is parsed (below)
|
||||
std::vector<float> req_bias;
|
||||
std::string bias_error;
|
||||
@@ -19484,8 +19631,10 @@ int main(int argc, char** argv) {
|
||||
return imgs_below(req_imgs, L) == pre_imgs;
|
||||
};
|
||||
const bool want_cvec = strata::kernels::cvec().loaded() ? req_cvec != 0 : true;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const uint64_t want_loop_key =
|
||||
strata::kernels::loop_cache_key(req_loop_passes, req_loop_cd_omega, req_loop_cd_thinking);
|
||||
#endif
|
||||
// the last request's final commit may still be running on the verifier's stream (set_commit_async):
|
||||
// everything below reads, restores or zeroes the session from other streams and the host (the end of the
|
||||
// last request waited already; this covers a request that ended on an error path)
|
||||
@@ -19619,7 +19768,11 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
int64_t resume = 0;
|
||||
bool from_live = false;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (o.prompt_cache > 0 && want_cvec == cvec_cached && want_loop_key == loop_key_cached) {
|
||||
#else
|
||||
if (o.prompt_cache > 0 && want_cvec == cvec_cached) {
|
||||
#endif
|
||||
if (live_ok && starts_with(live, live_imgs)) { resume = (int64_t) live.size(); from_live = true; }
|
||||
for (const ConvCheckpoint& c : checks)
|
||||
if ((int64_t) c.ids.size() > resume && starts_with(c.ids, c.imgs)) {
|
||||
@@ -19633,7 +19786,11 @@ int main(int argc, char** argv) {
|
||||
// of a slot's checkpoint this request moved into `checks` (from the slot it is admitted to), -1: none
|
||||
int lent_slot = -1;
|
||||
int64_t moved_in = -1;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (slot_ckpt_move_env() && o.prompt_cache > 0 && want_cvec == cvec_cached && want_loop_key == loop_key_cached && live_ok)
|
||||
#else
|
||||
if (slot_ckpt_move_env() && o.prompt_cache > 0 && want_cvec == cvec_cached && live_ok)
|
||||
#endif
|
||||
for (int b = 0; b < (int) bs.size(); ++b) {
|
||||
if (bs[(size_t) b].cvec != cvec_cached || bs[(size_t) b].tainted) continue; // (#879)
|
||||
for (const ConvCheckpoint& c : bs[(size_t) b].checks) {
|
||||
@@ -19713,7 +19870,11 @@ int main(int argc, char** argv) {
|
||||
k + 1, (long long) (k < now.size() ? now[k].start : -1));
|
||||
}
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const auto parked = conversations.best(ids, req_imgs, want_cvec, want_loop_key);
|
||||
#else
|
||||
const auto parked = conversations.best(ids, req_imgs, want_cvec);
|
||||
#endif
|
||||
// STRATA_SLOT_READ (opt-in: =1, with STRATA_PIPED_MTP; asked for by the server with slot_read=1, which then
|
||||
// takes DONE and BADM without a token): the next turn of the conversation an idle slot holds is read IN
|
||||
// that slot - its new tokens as rows of the slot's own windows in the pipelined groups, all kept - instead
|
||||
@@ -19864,7 +20025,11 @@ int main(int argc, char** argv) {
|
||||
const int64_t floor_tokens = std::max({resume, slot_tokens, parked.tokens});
|
||||
int64_t held = 0;
|
||||
spill_writer->for_each_held([&](const strata::core::SavedConversation& s) {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
held = std::max(held, strata::core::conversation_image_prefix(s, ids, req_imgs, want_cvec, want_loop_key).first);
|
||||
#else
|
||||
held = std::max(held, strata::core::conversation_image_prefix(s, ids, req_imgs, want_cvec).first);
|
||||
#endif
|
||||
});
|
||||
if (held > floor_tokens) {
|
||||
const auto t0 = Clock::now();
|
||||
@@ -19873,7 +20038,11 @@ int main(int argc, char** argv) {
|
||||
size_t at = SIZE_MAX;
|
||||
std::pair<int64_t, bool> m{0, false};
|
||||
for (size_t i = 0; i < done.size(); ++i) {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const auto mi = strata::core::conversation_image_prefix(done[i].item, ids, req_imgs, want_cvec, want_loop_key);
|
||||
#else
|
||||
const auto mi = strata::core::conversation_image_prefix(done[i].item, ids, req_imgs, want_cvec);
|
||||
#endif
|
||||
if (mi.first > m.first) { m = mi; at = i; }
|
||||
}
|
||||
if (at != SIZE_MAX && m.first > floor_tokens) {
|
||||
@@ -19890,7 +20059,11 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
spill_reclaim(); // what the writer finished meanwhile is on the SSD tier: its RAM goes back before this request parks
|
||||
if (!from_ssd && spill.enabled() && spill.size() > 0 && stages.empty()) {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const auto sp = spill.best(ids, req_imgs, want_cvec, want_loop_key);
|
||||
#else
|
||||
const auto sp = spill.best(ids, req_imgs, want_cvec);
|
||||
#endif
|
||||
const auto sp_entry = sp.tokens > 0 ? spill.find(sp.serial) : std::nullopt;
|
||||
if (sp_entry && sp.tokens > std::max({resume, slot_tokens, parked.tokens}) && spill_id) {
|
||||
const auto t0 = Clock::now();
|
||||
@@ -20034,7 +20207,11 @@ int main(int argc, char** argv) {
|
||||
// conversation, a parked one coming back, a batch slot, a borrow, another cvec) parks as before.
|
||||
bool reply_replaced = false;
|
||||
if (!from_live && !incoming && !borrow && slot_source < 0 && lent_slot < 0 && live_ok && !pin_sibling &&
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
want_cvec == cvec_cached && want_loop_key == loop_key_cached && resume > 0) {
|
||||
#else
|
||||
want_cvec == cvec_cached && resume > 0) {
|
||||
#endif
|
||||
std::vector<int64_t> live_pictures;
|
||||
for (const ImgKey& k : live_imgs) live_pictures.push_back((int64_t) k.start);
|
||||
for (const ConvCheckpoint& c : checks)
|
||||
@@ -20053,7 +20230,11 @@ int main(int argc, char** argv) {
|
||||
// it still holds this conversation's prefix, and re-parking writes the whole snapshot only for the next
|
||||
// turn to drop it again. 0 (the default) parks every turn, as before.
|
||||
const size_t park_held = o.conversation_cache_park_gain > 0
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
? conversations.parked_prefix_tokens(live, live_imgs, checks, cvec_cached, loop_key_cached)
|
||||
#else
|
||||
? conversations.parked_prefix_tokens(live, live_imgs, checks, cvec_cached)
|
||||
#endif
|
||||
: 0;
|
||||
const bool park_worth = o.conversation_cache_park_gain <= 0 || park_held == 0 ||
|
||||
(int64_t) live.size() >= (int64_t) park_held + o.conversation_cache_park_gain;
|
||||
@@ -20079,15 +20260,23 @@ int main(int argc, char** argv) {
|
||||
"live; %lld past the resume point are not kept)\n",
|
||||
(long long) resume, (long long) live.size(), (long long) (live.size() - resume));
|
||||
// STRATA_PARK_ASYNC: the checkpoints this request drops after its park - all of them when another state
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
// comes in (a parked conversation, a borrow, a batch slot) or the cvec / loop mode changes, else the ones off this
|
||||
#else
|
||||
// comes in (a parked conversation, a borrow, a batch slot) or the cvec changes, else the ones off this
|
||||
#endif
|
||||
// prompt's path (off_path below) - move into the parked image
|
||||
struct ParkDrops {
|
||||
std::function<bool(const ConvCheckpoint&)>& f;
|
||||
~ParkDrops() { f = nullptr; }
|
||||
} park_drops_reset{park_drops};
|
||||
if (park_async_env()) {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const bool drops_all = incoming.has_value() || borrow || slot_source >= 0 || want_cvec != cvec_cached ||
|
||||
want_loop_key != loop_key_cached;
|
||||
#else
|
||||
const bool drops_all = incoming.has_value() || borrow || slot_source >= 0 || want_cvec != cvec_cached;
|
||||
#endif
|
||||
park_drops = [&, drops_all](const ConvCheckpoint& c) {
|
||||
return drops_all || (int64_t) c.ids.size() > resume || !starts_with(c.ids, c.imgs);
|
||||
};
|
||||
@@ -20100,7 +20289,11 @@ int main(int argc, char** argv) {
|
||||
if (swap_block_bytes != 0 && incoming && !from_ssd && taken.live &&
|
||||
taken.tokens == (int64_t) incoming->live.ids.size() &&
|
||||
!borrow && !keep_branch && !reply_replaced && park_worth && live_ok && !live.empty() &&
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
conversations.wants((int64_t) live.size()) && !conversations.has_reuse_for(live, loop_key_cached) &&
|
||||
#else
|
||||
conversations.wants((int64_t) live.size()) && !conversations.has_reuse_for(live) &&
|
||||
#endif
|
||||
stages.empty() && o.batch == 0 && !kvg.on) {
|
||||
swap_t0 = Clock::now();
|
||||
struct SwapStage {
|
||||
@@ -20108,7 +20301,11 @@ int main(int argc, char** argv) {
|
||||
~SwapStage() { strata::core::progress_at("request"); strata::core::progress_beat(); }
|
||||
} swap_stage;
|
||||
try {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
const strata::core::ConversationView view{live, live_imgs, checks, cvec_cached, loop_key_cached};
|
||||
#else
|
||||
const strata::core::ConversationView view{live, live_imgs, checks, cvec_cached};
|
||||
#endif
|
||||
strata::core::ConversationSwapPlan plan;
|
||||
if (strata::core::conversation_snapshot_swap_prepare(plan, *incoming, view, ss, g,
|
||||
use_mtp ? &mtp.kv_state() : nullptr, swap_block_bytes, err)) {
|
||||
@@ -20317,7 +20514,9 @@ int main(int argc, char** argv) {
|
||||
for (const ConvCheckpoint& c : checks)
|
||||
if ((int64_t) c.ids.size() == parked.tokens) { live = c.ids; live_imgs = c.imgs; }
|
||||
cvec_cached = donor->cvec;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
loop_key_cached = donor->loop_key;
|
||||
#endif
|
||||
resume = parked.tokens;
|
||||
from_live = false;
|
||||
conversations.count_borrow();
|
||||
@@ -20372,14 +20571,20 @@ int main(int argc, char** argv) {
|
||||
if (!strata::core::conversation_checkpoints_merge(std::move(cs), checks)) checks.clear();
|
||||
}
|
||||
cvec_cached = incoming->cvec;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
loop_key_cached = incoming->loop_key;
|
||||
#endif
|
||||
resume = taken.tokens; // (parked.tokens unless the image came from the SSD tier)
|
||||
from_live = taken.live;
|
||||
if (!swapped_parked && std::getenv("STRATA_SNAPSHOT_FULL_CAPTURE") == nullptr) {
|
||||
std::vector<std::vector<strata::core::ConversationKv>> stage_kv; // every stage's, for its next park
|
||||
for (auto& si : incoming->stage_images) stage_kv.push_back(std::move(si.kv));
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
conversations.retain(std::move(incoming->kv), int64_t(live.size()), std::move(stage_kv), live,
|
||||
loop_key_cached);
|
||||
#else
|
||||
conversations.retain(std::move(incoming->kv), int64_t(live.size()), std::move(stage_kv), live);
|
||||
#endif
|
||||
}
|
||||
incoming.reset(); // Running-state/checkpoint copies are no longer needed.
|
||||
lent_slot = -1; // (the main session holds the parked conversation now)
|
||||
@@ -20388,11 +20593,17 @@ int main(int argc, char** argv) {
|
||||
std::chrono::duration<double, std::milli>(Clock::now() - t0).count(),
|
||||
conversations.size(), conversations.bytes());
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (want_cvec != cvec_cached || want_loop_key != loop_key_cached) {
|
||||
#else
|
||||
if (want_cvec != cvec_cached) {
|
||||
#endif
|
||||
live_ok = false;
|
||||
checks.clear();
|
||||
cvec_cached = want_cvec;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
loop_key_cached = want_loop_key;
|
||||
#endif
|
||||
}
|
||||
if (strata::kernels::cvec().loaded()) strata::kernels::cvec_set_enabled(want_cvec);
|
||||
// this request rewrites every cell from `resume` on, so a checkpoint past it (or not on this prompt's
|
||||
@@ -20755,20 +20966,24 @@ int main(int argc, char** argv) {
|
||||
std::fflush(stdout);
|
||||
return true;
|
||||
};
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
auto think_before = [&](int64_t pos) {
|
||||
return req_la.loaded()
|
||||
? strata::kernels::loop_think_state_after(ids.data(), pos, req_la.think_start_id,
|
||||
req_la.think_end_id, false)
|
||||
: false;
|
||||
};
|
||||
#endif
|
||||
// tokens [a, b) through the windows: commit all of them, then give the draft layer their residuals
|
||||
auto read_windows = [&](int64_t a, int64_t b, std::string& e) -> bool {
|
||||
// (STRATA_LOGPOS reads every window's logits: the serial loop)
|
||||
// the pipelined prompt read stays two-stage (three or more stages read serially; only the decode overlaps)
|
||||
if (pipe && n_stages == 2 && pl_pw >= 1 && std::getenv("STRATA_LOGPOS") == nullptr) return read_windows_pl(a, b, e);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (req_la.loaded() &&
|
||||
!ver.set_loop_request(req_loop_passes, req_loop_cd_omega, req_loop_cd_thinking, think_before(a), e))
|
||||
return false;
|
||||
#endif
|
||||
strata::core::progress_at("reading the prompt (verify windows), from token", a); // #217: not "batched"
|
||||
// every token is committed and the picks are discarded: no head sampling (see set_head_sampling)
|
||||
struct NoHeadSampling {
|
||||
@@ -21190,12 +21405,16 @@ int main(int argc, char** argv) {
|
||||
segs.push_back(sg);
|
||||
used.push_back(&c);
|
||||
}
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (segs.size() < 2) {
|
||||
if (req_la.loaded())
|
||||
sp.set_loop_request(req_loop_passes, req_loop_cd_omega, req_loop_cd_thinking,
|
||||
think_before(lead.pos0));
|
||||
return sp.run(lead.tokens, lead.n, lead.pos0, ce);
|
||||
}
|
||||
#else
|
||||
if (segs.size() < 2) return sp.run(lead.tokens, lead.n, lead.pos0, ce);
|
||||
#endif
|
||||
for (const CoReq* c : used) {
|
||||
park_slot(c->slot); // the conversation the slot holds, before this read overwrites it
|
||||
for (size_t k = 0; k < bslot_ss.size(); ++k) {
|
||||
@@ -21213,9 +21432,11 @@ int main(int argc, char** argv) {
|
||||
if (ce.rfind("prefill: multi-prompt read declined", 0) == 0) { // nothing was read: the usual run
|
||||
std::fprintf(stderr, "strata batch: co-read of %zu prompt(s) declined (%s)\n", used.size(), ce.c_str());
|
||||
ce.clear();
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (req_la.loaded())
|
||||
sp.set_loop_request(req_loop_passes, req_loop_cd_omega, req_loop_cd_thinking,
|
||||
think_before(lead.pos0));
|
||||
#endif
|
||||
return sp.run(lead.tokens, lead.n, lead.pos0, ce);
|
||||
}
|
||||
return false;
|
||||
@@ -21243,9 +21464,11 @@ int main(int argc, char** argv) {
|
||||
co_tried = true;
|
||||
run_ok = co_run(q, r, e);
|
||||
} else {
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (req_la.loaded())
|
||||
sp.set_loop_request(req_loop_passes, req_loop_cd_omega, req_loop_cd_thinking,
|
||||
think_before(q));
|
||||
#endif
|
||||
run_ok = sp.run(ids.data() + q, r - q, q, e);
|
||||
}
|
||||
if (run_ok && q == 0 && sp.kva_floor() > 0) { // KVA approximated the start of this prompt: remember which tokens
|
||||
@@ -21556,9 +21779,11 @@ int main(int argc, char** argv) {
|
||||
conc_thread_cpus();
|
||||
try { // (an exception leaving a std::thread ends the process: a failed read instead)
|
||||
if (planned) sp.force_chunk(plan.chunk, plan_single, plan.chunked);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (req_la.loaded())
|
||||
sp.set_loop_request(req_loop_passes, req_loop_cd_omega, req_loop_cd_thinking,
|
||||
think_before(q0));
|
||||
#endif
|
||||
read_ok = sp.run(ids.data() + q0, b0 - q0, q0, read_err);
|
||||
} catch (const std::exception& x) {
|
||||
read_ok = false;
|
||||
@@ -23029,11 +23254,13 @@ int main(int argc, char** argv) {
|
||||
auto draft_df2 = [&](int) -> bool { return false; };
|
||||
#endif
|
||||
if (df2_trace != nullptr) std::fprintf(df2_trace, "# request prompt %lld df2 %d spec %d\n", (long long) n, req_df2 ? 1 : 0, S_mtp);
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (req_la.loaded() && !pl_ran && !cancelled &&
|
||||
!ver.set_loop_request(req_loop_passes, req_loop_cd_omega, req_loop_cd_thinking, think_before(p), err)) {
|
||||
std::printf("ERR %s\n", err.c_str());
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
while (!pl_ran && !cancelled && produced_n < cap_now()) {
|
||||
int T = 1;
|
||||
policy.set_context(p);
|
||||
@@ -25182,6 +25409,7 @@ int main(int argc, char** argv) {
|
||||
return true;
|
||||
};
|
||||
int64_t p = spec_pos;
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
if (strata::kernels::loop_adapter().loaded()) {
|
||||
const auto& cli_la = strata::kernels::loop_adapter();
|
||||
const int64_t n_pr = std::min<int64_t>(p, (int64_t) o.tokens.size());
|
||||
@@ -25194,6 +25422,7 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
ver.set_loop_cd_in_thinking(cli_th);
|
||||
}
|
||||
#endif
|
||||
int32_t x = (int32_t) tok;
|
||||
std::vector<int32_t> drafts((size_t) o.spec, 0);
|
||||
std::vector<float> dprob((size_t) o.spec, 1.0f);
|
||||
|
||||
@@ -280,7 +280,10 @@ set(_kernel_files "")
|
||||
foreach(k IN LISTS _kernel_cu)
|
||||
list(APPEND _kernel_files src/kernels/cuda/${k}.cu)
|
||||
endforeach()
|
||||
strata_resolve(_kernels ${_kernel_files} src/kernels/ngram.cpp src/kernels/loop_adapter_stub.cpp src/ngram/ple_reader.cpp src/platform/direct_file.cpp)
|
||||
if(STRATA_WITH_LOOP_BUILD) # q4_lite only: the generic engine sources name the loop adapter entry points
|
||||
list(APPEND _kernel_files src/kernels/loop_adapter_stub.cpp)
|
||||
endif()
|
||||
strata_resolve(_kernels ${_kernel_files} src/kernels/ngram.cpp src/ngram/ple_reader.cpp src/platform/direct_file.cpp)
|
||||
list(APPEND _kernels ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/cuda/qsa_prompt_attn_xmx.dp.cpp) # port-only: the XMX prompt attention
|
||||
list(APPEND _kernels ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/cuda/qsa_prompt_attn_esimd.dp.cpp) # port-only: the int8 DPAS (ESIMD) prompt attention
|
||||
list(APPEND _kernels ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/cuda/weight_permute.dp.cpp)
|
||||
|
||||
@@ -314,10 +314,14 @@ bool fused_swiglu_q81_enabled() {
|
||||
|
||||
void shared_expert_multi(int n_tok, const float* x, const uint16_t* x_bf16, const NativeSharedWeights& nw,
|
||||
const uint16_t* gate_inp_bf16, float* gate, float* up, float* g, float* out, int64_t n_embd,
|
||||
#ifdef STRATA_WITH_LOOP
|
||||
int64_t n_ff, void* stream, const void* x_q8_1_ready, int lfuse, int loop_pass,
|
||||
int64_t loop_layer) {
|
||||
(void) loop_pass;
|
||||
(void) loop_layer;
|
||||
#else
|
||||
int64_t n_ff, void* stream, const void* x_q8_1_ready, int lfuse) {
|
||||
#endif
|
||||
const bool gate_deferred = (lfuse & 1) != 0, pair = (lfuse & 2) != 0;
|
||||
if (n_tok < 1 || n_tok > 8 || !nw.q8_1 || !nw.gate_data || !nw.up_data || !nw.down_data || !stream)
|
||||
throw std::invalid_argument("shared_expert_multi: needs 1..8 tokens, native weights, scratch and a stream");
|
||||
|
||||
Reference in new issue
Block a user