MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / load_tensors

Method load_tensors

subprojects/llama.cpp/src/llama-model.cpp:2473–7129  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2471}
2472
2473bool llama_model::load_tensors(llama_model_loader & ml) {
2474 const auto & split_mode = params.split_mode;
2475 const auto & use_mlock = params.use_mlock;
2476 const auto & tensor_split = params.tensor_split;
2477
2478 const int n_layer = hparams.n_layer;
2479 const int n_gpu_layers = this->n_gpu_layers();
2480
2481 const bool use_mmap_buffer = true;
2482
2483 LLAMA_LOG_INFO("%s: loading model tensors, this can take a while... (mmap = %s, direct_io = %s)\n",
2484 __func__, ml.use_mmap ? "true" : "false", ml.use_direct_io ? "true" : "false");
2485
2486 // build a list of buffer types for the CPU and GPU devices
2487 pimpl->cpu_buft_list = make_cpu_buft_list(devices, params.use_extra_bufts, params.no_host);
2488 for (auto * dev : devices) {
2489 buft_list_t buft_list = make_gpu_buft_list(dev, split_mode, tensor_split);
2490 // add CPU buffer types as a fallback
2491 buft_list.insert(buft_list.end(), pimpl->cpu_buft_list.begin(), pimpl->cpu_buft_list.end());
2492 pimpl->gpu_buft_list.emplace(dev, std::move(buft_list));
2493 }
2494
2495 ggml_backend_dev_t cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
2496 if (cpu_dev == nullptr) {
2497 throw std::runtime_error(format("%s: no CPU backend found", __func__));
2498 }
2499
2500 // calculate the split points
2501 bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + n_devices(), [](float x) { return x == 0.0f; });
2502 std::vector<float> splits(n_devices());
2503 if (all_zero) {
2504 // default split, by free memory
2505 for (size_t i = 0; i < n_devices(); ++i) {
2506 ggml_backend_dev_t dev = devices[i];
2507 size_t total;
2508 size_t free;
2509 ggml_backend_dev_memory(dev, &free, &total);
2510
2511 // devices can return 0 bytes for free and total memory if they do not
2512 // have any to report. in this case, we will use the host memory as a fallback
2513 // fixes: https://github.com/ggml-org/llama.cpp/issues/18577
2514 if (free == 0 && total == 0) {
2515 ggml_backend_dev_memory(cpu_dev, &free, &total);
2516 }
2517 splits[i] = free;
2518 }
2519 } else {
2520 std::copy(tensor_split, tensor_split + n_devices(), splits.begin());
2521 }
2522
2523 // sum and normalize the splits to get the split points
2524 float split_sum = 0.0f;
2525 for (size_t i = 0; i < n_devices(); ++i) {
2526 split_sum += splits[i];
2527 splits[i] = split_sum;
2528 }
2529 for (size_t i = 0; i < n_devices(); ++i) {
2530 splits[i] /= split_sum;

Callers 1

llama_model_loadFunction · 0.45

Calls 15

n_gpu_layersMethod · 0.95
make_cpu_buft_listFunction · 0.85
make_gpu_buft_listFunction · 0.85
ggml_backend_dev_by_typeFunction · 0.85
formatFunction · 0.85
ggml_backend_dev_memoryFunction · 0.85
maxFunction · 0.85
minFunction · 0.85
ggml_backend_dev_nameFunction · 0.85
ggml_tensor_overheadFunction · 0.85
ggml_initFunction · 0.85
ggml_nbytesFunction · 0.85

Tested by

no test coverage detected