Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/llama.h
Original file line number Diff line number Diff line change
Expand Up @@ -2112,6 +2112,10 @@ extern "C" {
// The optimizer enforces both gates. Nonfresh rejection preserves existing graph ownership.
// This is not a journal commit or adoption.
LLAMA_API bool llama_opt_prepare_restore(struct llama_context * ctx, struct llama_batch retained);
// The labelled batch llama_opt_prepare_restore takes (tokens, positions, one sequence, logits, labels), allocated for
// n_tokens and freed by its pair: the opt path's own allocator, since the general llama_batch_init is deprecated.
LLAMA_API struct llama_batch llama_opt_batch_init(int32_t n_tokens);
LLAMA_API void llama_opt_batch_free(struct llama_batch batch);
LLAMA_API bool llama_opt_state_restore(struct llama_context * ctx, struct llama_adapter_lora * shadow,
FILE * weights, FILE * optimizer, const char * binding);

Expand Down
8 changes: 8 additions & 0 deletions src/llama-context-opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,14 @@ bool llama_opt_prepare_restore(llama_context * ctx, llama_batch retained) {
return ctx && ctx->opt_prepare_restore(retained);
}

llama_batch llama_opt_batch_init(int32_t n_tokens) {
return llama_batch_init_impl(n_tokens, 0, 1);
}

void llama_opt_batch_free(llama_batch batch) {
llama_batch_free_impl(batch);
}

bool llama_context::opt_state_restore(llama_adapter_lora * shadow,
FILE * weights, FILE * optimizer, const char * binding) {
if (opt_reply || !shadow || opt_shadow != shadow || !opt_ctx) { return false; }
Expand Down
18 changes: 9 additions & 9 deletions tests/test-kv-cell-index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ void cost(uint32_t n) {
} // namespace

// Counts this binary's allocations while a workload runs (counting is off otherwise).
void * operator new(std::size_t size) {
static void * counted_alloc(std::size_t size) {
if (counting.load(std::memory_order_relaxed)) {
allocations.fetch_add(1, std::memory_order_relaxed);
}
Expand All @@ -268,14 +268,14 @@ void * operator new(std::size_t size) {
}
throw std::bad_alloc();
}

void operator delete(void * p) noexcept {
std::free(p);
}

void operator delete(void * p, std::size_t) noexcept {
std::free(p);
}
// the whole family, through one helper, as test-persona-blocks replaces them (which builds clean under
// the Linux CI's GCC -Werror; this file's direct malloc in operator new tripped -Wmismatched-new-delete)
void * operator new(std::size_t size) { return counted_alloc(size); }
void * operator new[](std::size_t size) { return counted_alloc(size); }
void operator delete(void * p) noexcept { std::free(p); }
void operator delete[](void * p) noexcept { std::free(p); }
void operator delete(void * p, std::size_t) noexcept { std::free(p); }
void operator delete[](void * p, std::size_t) noexcept { std::free(p); }

int main(int argc, char ** /* argv: only the flag count is read */) {
for (uint32_t seed = 1; seed <= 8 && !failures; ++seed) {
Expand Down
19 changes: 10 additions & 9 deletions tests/test-moe-residency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
#include <sstream>
#include <filesystem>

// A container file of its own for one test, under the system temp dir. tmpnam is deprecated and its result unchecked
// failed -Werror on Linux; a fixed name per test is enough here (each test removes or rewrites its own).
static std::string temp_container(const char * tag) {
return (std::filesystem::temp_directory_path() / (std::string("test-moe-residency-") + tag + ".moec")).string();
}

// ------------------------------------------------------------------------------------------------
// tiny test harness
// ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -181,8 +187,7 @@ static void test_derive_budget_from_system_capabilities() {
static void test_container_roundtrip_real_io() {
const uint64_t stride = ggml_moe::moec_align_up(8192); // small aligned record for the test
ggml_moe::ContainerHeader h = make_header(3, 4, stride); // 3 layers x 4 experts
char path[L_tmpnam]; std::tmpnam(path);
std::string p = std::string(path) + ".moec";
std::string p = temp_container("roundtrip");

// marker byte for (layer, expert) — distinct per record, written at both ends of the payload.
auto marker = [](uint32_t L, uint32_t E) -> unsigned char { return (unsigned char)(1 + L * 4 + E); };
Expand Down Expand Up @@ -226,8 +231,7 @@ static void test_container_roundtrip_real_io() {
static void test_container_fetcher_reads_right_expert() {
const uint64_t stride = ggml_moe::moec_align_up(8192);
ggml_moe::ContainerHeader h = make_header(3, 5, stride);
char path[L_tmpnam]; std::tmpnam(path);
std::string p = std::string(path) + ".moec";
std::string p = temp_container("fetcher");
auto marker = [](uint32_t L, uint32_t E) -> unsigned char { return (unsigned char)(7 + L * 5 + E); };
{ // pack
std::ofstream out(p, std::ios::binary);
Expand Down Expand Up @@ -266,8 +270,7 @@ struct SyntheticSource : ggml_moe::ExpertSource {
static void test_packer_fetcher_end_to_end() {
const uint64_t stride = ggml_moe::moec_align_up(4096);
const uint32_t layers = 4, experts = 6;
char path[L_tmpnam]; std::tmpnam(path);
std::string p = std::string(path) + ".moec";
std::string p = temp_container("packer-fetcher");

SyntheticSource src;
const bool packed = ggml_moe::moec_pack(p.c_str(), layers, experts, stride, ggml_moe::MOEC_Q_RVQ3, src);
Expand Down Expand Up @@ -354,8 +357,7 @@ struct WexpSource : ggml_moe::ExpertSource {
static void test_wexp_source_packs_valid_records() {
const uint64_t stride = ggml_moe::moec_align_up(4096);
const uint32_t layers = 3, experts = 4;
char path[L_tmpnam]; std::tmpnam(path);
std::string p = std::string(path) + ".moec";
std::string p = temp_container("wexp-source");
WexpSource src((uint32_t) (stride / 4096));
CHECK(ggml_moe::moec_pack(p.c_str(), layers, experts, stride, ggml_moe::MOEC_Q_RVQ3, src),
"pack a container of real WEXP records");
Expand Down Expand Up @@ -482,7 +484,6 @@ static void test_iq2_source_packs_and_reads_back() {
CHECK(ggml_moe::moec_pack_dir(dir.string().c_str(), "kimi-k3-iq2", ggml_moe::MOEC_Q_IQ2,
layers, experts, record_bytes, top_k * layers, top_k, src), "pack IQ2 container");

ggml_moe::ContainerHeader h = make_header(layers, experts, record_bytes);
bool ok = true;
for (uint32_t L = 0; L < layers && ok; L++) {
std::ifstream in(dir / ("experts-L" + std::to_string(L) + ".bin"), std::ios::binary);
Expand Down
28 changes: 7 additions & 21 deletions tests/test-state-restore-streams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@
#include <cstdio>
#include <vector>

// upstream removed common_batch_add with the move to llama_batch_ext; the test keeps the public llama_batch
static void state_batch_add(llama_batch & batch, llama_token id, llama_pos pos, llama_seq_id seq, bool logits) {
GGML_ASSERT(batch.seq_id[batch.n_tokens] && "llama_batch size exceeded");
batch.token [batch.n_tokens] = id;
batch.pos [batch.n_tokens] = pos;
batch.n_seq_id[batch.n_tokens] = 1;
batch.seq_id [batch.n_tokens][0] = seq;
batch.logits [batch.n_tokens] = logits;
batch.n_tokens++;
}

static llama_context * make_ctx(llama_model * model, const common_params & base, int n_parallel, bool unified) {
common_params params = base;
params.n_parallel = n_parallel;
Expand All @@ -39,14 +28,12 @@ static llama_context * make_ctx(llama_model * model, const common_params & base,

// decode `tok` at `pos` on `seq` and copy the logits out
static bool next_logits(llama_context * ctx, llama_token tok, int pos, llama_seq_id seq, int n_vocab, std::vector<float> & out) {
llama_batch batch = llama_batch_init(1, 0, 1);
state_batch_add(batch, tok, pos, seq, true);
const bool ok = llama_decode(ctx, batch) == 0;
llama_batch_free(batch);
if (!ok) {
common_batch batch(ctx);
batch.add(tok, pos, seq, true);
if (llama_process(ctx, LLAMA_PROCESS_TYPE_DECODE, batch.get()) != 0) {
return false;
}
const float * logits = llama_get_logits_ith(ctx, -1);
const float * logits = llama_get_logits_ith(ctx, 0);
out.assign(logits, logits + n_vocab);
return true;
}
Expand Down Expand Up @@ -89,15 +76,14 @@ int main(int argc, char ** argv) {
return 1;
}
{
llama_batch batch = llama_batch_init(n_prompt, 0, 1);
common_batch batch(writer);
for (int i = 0; i < n_prompt; ++i) {
state_batch_add(batch, (llama_token) (1 + i % 7), i, seq, false);
batch.add((llama_token) (1 + i % 7), i, seq, false);
}
if (llama_decode(writer, batch)) {
if (llama_process(writer, LLAMA_PROCESS_TYPE_DECODE, batch.get())) {
fprintf(stderr, "%s : failed to decode the prompt\n", __func__);
return 1;
}
llama_batch_free(batch);
}

std::vector<uint8_t> state(llama_state_seq_get_size(writer, seq));
Expand Down
Loading
Loading