Skip to content

Commit d8a24cc

Browse files
authored
fit : wrap llama_device_memory_data (ggml-org#24522)
1 parent c34b922 commit d8a24cc

3 files changed

Lines changed: 40 additions & 14 deletions

File tree

common/fit.cpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class common_params_fit_exception : public std::runtime_error {
2626
using std::runtime_error::runtime_error;
2727
};
2828

29-
std::vector<llama_device_memory_data> common_get_device_memory_data(
29+
static std::vector<llama_device_memory_data> common_get_device_memory_data_impl(
3030
const char * path_model,
3131
const llama_model_params * mparams,
3232
const llama_context_params * cparams,
@@ -150,6 +150,29 @@ std::vector<llama_device_memory_data> common_get_device_memory_data(
150150
return ret;
151151
}
152152

153+
common_device_memory_data_vec common_get_device_memory_data(
154+
const char * path_model,
155+
const llama_model_params * mparams,
156+
const llama_context_params * cparams,
157+
std::vector<ggml_backend_dev_t> & devs,
158+
uint32_t & hp_ngl,
159+
uint32_t & hp_n_ctx_train,
160+
uint32_t & hp_n_expert,
161+
ggml_log_level log_level) {
162+
std::vector<llama_device_memory_data> impl = common_get_device_memory_data_impl(
163+
path_model, mparams, cparams, devs, hp_ngl, hp_n_ctx_train, hp_n_expert, log_level);
164+
165+
common_device_memory_data_vec ret(impl.size());
166+
for (size_t i = 0; i < impl.size(); i++) {
167+
ret[i].total = impl[i].total;
168+
ret[i].free = impl[i].free;
169+
ret[i].model = impl[i].mb.model;
170+
ret[i].context = impl[i].mb.context;
171+
ret[i].compute = impl[i].mb.compute;
172+
}
173+
return ret;
174+
}
175+
153176
static void common_params_fit_impl(
154177
const char * path_model, struct llama_model_params * mparams, struct llama_context_params * cparams,
155178
float * tensor_split, struct llama_model_tensor_buft_override * tensor_buft_overrides,
@@ -169,7 +192,7 @@ static void common_params_fit_impl(
169192
// step 1: get data for default parameters and check whether any changes are necessary in the first place
170193

171194
LOG_TRC("%s: getting device memory data for initial parameters:\n", __func__);
172-
const dmds_t dmds_full = common_get_device_memory_data(path_model, mparams, cparams, devs, hp_ngl, hp_nct, hp_nex, log_level);
195+
const dmds_t dmds_full = common_get_device_memory_data_impl(path_model, mparams, cparams, devs, hp_ngl, hp_nct, hp_nex, log_level);
173196
const size_t nd = devs.size(); // number of devices
174197

175198
std::vector<int64_t> margins; // this function uses int64_t rather than size_t for memory sizes to more conveniently handle deficits
@@ -304,7 +327,7 @@ static void common_params_fit_impl(
304327

305328
int64_t sum_projected_used_min_ctx = 0;
306329
cparams->n_ctx = n_ctx_min;
307-
const dmds_t dmds_min_ctx = common_get_device_memory_data(path_model, mparams, cparams, devs, hp_ngl, hp_nct, hp_nex, log_level);
330+
const dmds_t dmds_min_ctx = common_get_device_memory_data_impl(path_model, mparams, cparams, devs, hp_ngl, hp_nct, hp_nex, log_level);
308331
if (nd == 0) {
309332
sum_projected_used_min_ctx = dmds_min_ctx.back().mb.total();
310333
} else {
@@ -482,7 +505,7 @@ static void common_params_fit_impl(
482505
llama_model_params mparams_copy = *mparams;
483506
set_ngl_tensor_split_tbo(ngl_per_device, overflow_bufts, mparams_copy);
484507

485-
const dmds_t dmd_nl = common_get_device_memory_data(
508+
const dmds_t dmd_nl = common_get_device_memory_data_impl(
486509
path_model, &mparams_copy, cparams, devs, hp_ngl, hp_nct, hp_nex, log_level);
487510

488511
LOG_TRC("%s: memory for test allocation by device:\n", func_name);
@@ -510,7 +533,7 @@ static void common_params_fit_impl(
510533
mparams->tensor_buft_overrides = tensor_buft_overrides;
511534

512535
LOG_TRC("%s: getting device memory data with all MoE tensors moved to system memory:\n", __func__);
513-
const dmds_t dmds_cpu_moe = common_get_device_memory_data(
536+
const dmds_t dmds_cpu_moe = common_get_device_memory_data_impl(
514537
path_model, mparams, cparams, devs, hp_ngl, hp_nct, hp_nex, log_level);
515538

516539
for (size_t id = 0; id < nd; id++) {
@@ -940,7 +963,7 @@ void common_fit_print(
940963
uint32_t hp_nct = 0; // hparams.n_ctx_train
941964
uint32_t hp_nex = 0; // hparams.n_expert
942965

943-
auto dmd = common_get_device_memory_data(path_model, mparams, cparams, devs, hp_ngl, hp_nct, hp_nex, GGML_LOG_LEVEL_ERROR);
966+
auto dmd = common_get_device_memory_data_impl(path_model, mparams, cparams, devs, hp_ngl, hp_nct, hp_nex, GGML_LOG_LEVEL_ERROR);
944967
GGML_ASSERT(dmd.size() == devs.size() + 1);
945968

946969
for (size_t id = 0; id < devs.size(); id++) {

common/fit.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,18 @@ void common_fit_print(
3434

3535
void common_memory_breakdown_print(const llama_context * ctx);
3636

37-
// TODO: convert this to common_device_memory_data that wraps llama_device_memory_data
38-
// add API for accessing the internal `llama-ext.h` information
39-
struct llama_device_memory_data;
37+
struct common_device_memory_data {
38+
int64_t total;
39+
int64_t free;
40+
size_t model;
41+
size_t context;
42+
size_t compute;
43+
};
44+
45+
using common_device_memory_data_vec = std::vector<common_device_memory_data>;
4046

4147
// Load a model + context with no_alloc and return the per-device memory breakdown.
42-
std::vector<llama_device_memory_data> common_get_device_memory_data(
48+
common_device_memory_data_vec common_get_device_memory_data(
4349
const char * path_model,
4450
const llama_model_params * mparams,
4551
const llama_context_params * cparams,

tools/server/server-context.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -962,10 +962,7 @@ struct server_context_impl {
962962
}
963963

964964
for (size_t j = 0; j < devs.size(); ++j) {
965-
const size_t bytes =
966-
(measure_model_bytes ? dmd[j].mb.model : 0) +
967-
dmd[j].mb.context +
968-
dmd[j].mb.compute;
965+
const size_t bytes = (measure_model_bytes ? dmd[j].model : 0) + dmd[j].context + dmd[j].compute;
969966
total += bytes;
970967
for (size_t i = 0; i < tgt_devices.size(); i++) {
971968
if (tgt_devices[i] == devs[j]) {

0 commit comments

Comments
 (0)