llama : move no_alloc out of llama_hparams (#31141)
* llama : move no_alloc out of llama_hparams Assisted-by: dsh:llama.cpp/ggml-org/GLM-5.3-Flash-GGUF (2x DGX Spark (RDMA, TP=2)) * cont : clean-up * cont : clean-up Assisted-by: dsh:llama.cpp/ggml-org/GLM-5.3-Flash-GGUF (2x DGX Spark (RDMA, TP=2)) * tests : add a no_alloc case to test-llama-context Assisted-by: dsh:llama.cpp/ggml-org/GLM-5.3-Flash-GGUF (2x DGX Spark (RDMA, TP=2))
This commit is contained in:
1 parent
4b851fbee8
commit
a4699400e3
30 files changed
+266
-179
No files matched your search
@@ -4,6 +4,8 @@
|
||||
#include "llama-io.h"
|
||||
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
llama_memory_status llama_memory_status_combine(llama_memory_status s0, llama_memory_status s1) {
|
||||
@@ -64,6 +66,52 @@ bool llama_memory_status_is_fail(llama_memory_status status) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
// llama_memory_bufs
|
||||
//
|
||||
|
||||
ggml_backend_buffer_ptr llama_memory_bufs::alloc_buf(ggml_context * ctx, ggml_backend_buffer_type_t buft, bool no_alloc, const char * name) {
|
||||
ggml_backend_buffer_ptr buf;
|
||||
if (no_alloc) {
|
||||
buf.reset(ggml_backend_buft_alloc_buffer(buft, /*size =*/ 0)); // dummy buffer
|
||||
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
|
||||
t->buffer = buf.get(); // set dummy buffer so that the backend scheduler won't try to allocate the tensors
|
||||
}
|
||||
} else {
|
||||
buf.reset(ggml_backend_alloc_ctx_tensors_from_buft(ctx, buft)); // real buffer
|
||||
}
|
||||
if (!buf) {
|
||||
throw std::runtime_error(std::string("failed to allocate buffer for ") + name);
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
ggml_backend_buffer_t llama_memory_bufs::emplace(ggml_context_ptr ctx, ggml_backend_buffer_type_t buft, const char * name) {
|
||||
ggml_backend_buffer_ptr buf = alloc_buf(ctx.get(), buft, no_alloc, name);
|
||||
|
||||
// initialize the buffer to avoid NaNs in the padding
|
||||
ggml_backend_buffer_clear(buf.get(), 0);
|
||||
|
||||
bufs.emplace_back(std::move(ctx), std::move(buf));
|
||||
|
||||
return bufs.back().second.get();
|
||||
}
|
||||
|
||||
std::map<ggml_backend_buffer_type_t, size_t> llama_memory_bufs::memory_breakdown() const {
|
||||
std::map<ggml_backend_buffer_type_t, size_t> ret;
|
||||
for (const auto & [ctx, buf] : bufs) {
|
||||
ggml_backend_buffer_type_t buft = ggml_backend_buffer_get_type(buf.get());
|
||||
|
||||
if (no_alloc) {
|
||||
GGML_ASSERT(ggml_backend_buffer_get_base(buf.get()) == nullptr);
|
||||
ret[buft] += ggml_backend_alloc_ctx_tensors_from_buft_size(ctx.get(), buft);
|
||||
} else {
|
||||
ret[buft] += ggml_backend_buffer_get_size(buf.get());
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// collects the buffers of the tensors that a state write visits, without keeping the data
|
||||
struct llama_memory_buffers_writer : public llama_io_write_i {
|
||||
std::set<ggml_backend_buffer_t> buffers;
|
||||
@@ -84,6 +132,10 @@ struct llama_memory_buffers_writer : public llama_io_write_i {
|
||||
};
|
||||
|
||||
std::vector<ggml_backend_buffer_t> llama_memory_get_buffers(const llama_memory_t mem) {
|
||||
if (mem == nullptr) {
|
||||
return {};
|
||||
}
|
||||
|
||||
llama_memory_buffers_writer writer;
|
||||
|
||||
mem->state_write(writer);
|
||||
|
||||
Reference in new issue
Block a user