Commit 679d8677b for llama.cpp

commit 679d8677b2042e9ad2b6569b78127f5e1b0e0ffd
Author: Chipmunk <101038159+CHIPMUNK-T0T@users.noreply.github.com>
Date:   Thu Nov 26 00:48:55 2026 +0900

    llama : fix seq state restore with ON_DEVICE (#30318)

diff --git a/src/llama-context.cpp b/src/llama-context.cpp
index 4b00c3d38..89465d0ce 100644
--- a/src/llama-context.cpp
+++ b/src/llama-context.cpp
@@ -3860,28 +3860,28 @@ size_t llama_context::state_seq_get_data(llama_seq_id seq_id, uint8_t * dst, siz
 }

 size_t llama_context::state_seq_set_data(llama_seq_id seq_id, const uint8_t * src, size_t size, llama_state_seq_flags flags) {
-    std::unique_ptr<llama_io_read_i> io;
-    if (flags & LLAMA_STATE_SEQ_FLAGS_ON_DEVICE) {
-        // create a temporary io to read the magic and the src seq_id
-        io = std::make_unique<llama_io_read_host>(src, size);
+    std::unique_ptr<llama_io_read_i> io = std::make_unique<llama_io_read_host>(src, size);

-        uint32_t magic_read;
-        io->read(&magic_read, sizeof(magic_read));
-        if (io_magic != magic_read) {
-            throw std::runtime_error("wrong sequence state magic");
-        }
+    try {
+        if (flags & LLAMA_STATE_SEQ_FLAGS_ON_DEVICE) {
+            // read the magic and the src seq_id with the host io, then switch to the device io
+            uint32_t magic_read;
+            io->read(&magic_read, sizeof(magic_read));
+            if (io_magic != magic_read) {
+                throw std::runtime_error("wrong sequence state magic");
+            }

-        llama_seq_id seq_id_read;
-        io->read(&seq_id_read, sizeof(seq_id_read));
+            llama_seq_id seq_id_read;
+            io->read(&seq_id_read, sizeof(seq_id_read));

-        GGML_ASSERT(mem_storage.find(seq_id_read) != mem_storage.end());
+            const auto it = mem_storage.find(seq_id_read);
+            if (it == mem_storage.end()) {
+                throw std::runtime_error(format("no device state stored for seq_id %d", seq_id_read));
+            }

-        io = std::make_unique<llama_io_read_device>(src, size, mem_storage[seq_id_read]);
-    } else {
-        io = std::make_unique<llama_io_read_host>(src, size);
-    }
+            io = std::make_unique<llama_io_read_device>(src, size, it->second);
+        }

-    try {
         uint32_t magic_read;
         io->read(&magic_read, sizeof(magic_read));
         if (io_magic != magic_read) {
diff --git a/tests/llama-context/cases-kv.cpp b/tests/llama-context/cases-kv.cpp
index 4d2970e9d..bebbec632 100644
--- a/tests/llama-context/cases-kv.cpp
+++ b/tests/llama-context/cases-kv.cpp
@@ -11,6 +11,7 @@

 #include <algorithm>
 #include <cstdio>
+#include <cstring>
 #include <fstream>
 #include <functional>
 #include <iterator>
@@ -473,6 +474,29 @@ struct case_state_restore_failure : ctx_test {

                 return nread == 0;
             }},
+            { "device", [&]() {
+                std::vector<uint8_t> state(llama_state_seq_get_size_ext(ctx.get(), 0, LLAMA_STATE_SEQ_FLAGS_ON_DEVICE));
+                GGML_ASSERT(llama_state_seq_get_data_ext(ctx.get(), state.data(), state.size(), 0, LLAMA_STATE_SEQ_FLAGS_ON_DEVICE) == state.size());
+                llama_memory_seq_rm(mem, 0, -1, -1);
+
+                // the state starts with the magic and the source seq_id; the seq_id selects the stored device data
+                const llama_seq_id seq_id_unknown = 3;
+
+                std::vector<std::vector<uint8_t>> inputs(5, state);
+                inputs[0][0] ^= 0xff;
+                inputs[1].resize(sizeof(uint32_t) / 2);
+                inputs[2].resize(sizeof(uint32_t) + sizeof(llama_seq_id) / 2);
+                memcpy(inputs[3].data() + sizeof(uint32_t), &seq_id_unknown, sizeof(seq_id_unknown));
+                inputs[4].resize(sizeof(uint32_t) + sizeof(llama_seq_id) + 1);
+
+                for (const auto & data : inputs) {
+                    if (llama_state_seq_set_data_ext(ctx.get(), data.data(), data.size(), 0, LLAMA_STATE_SEQ_FLAGS_ON_DEVICE) != 0) {
+                        return false;
+                    }
+                }
+
+                return true;
+            }},
         };

         for (const auto & [name, restore_failed] : sub_cases) {