-
Notifications
You must be signed in to change notification settings - Fork 44
llama: map each contiguous run of a context's tensors, not one span over all of them #152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: base/upstream-de8656bd9
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1738,19 +1738,26 @@ bool llama_model_base::load_tensors(llama_model_loader & ml) { | |
| // this is important for metal with apple silicon: if the entire model could be mapped to a metal buffer, | ||
| // then we could just use metal for all layers | ||
| // this allows using partial offloading when the model size exceeds the metal buffer size, but not the RAM size | ||
| // a tensor of another buffer type can sit between this context's, and one span | ||
| // over them would map it too, so map each contiguous run separately | ||
| void * addr = nullptr; | ||
| size_t first, last; // NOLINT | ||
| ml.get_mapping_range(&first, &last, &addr, idx, ctx); | ||
| if (first >= last) { | ||
| std::vector<std::pair<size_t, size_t>> ranges; | ||
| ml.get_mapping_ranges(ranges, &addr, idx, ctx); | ||
| if (ranges.empty()) { | ||
| continue; | ||
| } | ||
| const size_t max_size = ggml_get_max_tensor_size(ctx); | ||
| ggml_backend_buffer_t buf = ggml_backend_dev_buffer_from_host_ptr(dev, (char *) addr + first, last - first, max_size); | ||
| if (buf == nullptr) { | ||
| throw std::runtime_error(format("unable to allocate %s buffer", ggml_backend_buft_name(buft))); | ||
| for (const auto & [first, last] : ranges) { | ||
| if (first >= last) { | ||
| continue; | ||
| } | ||
| ggml_backend_buffer_t buf = ggml_backend_dev_buffer_from_host_ptr(dev, (char *) addr + first, last - first, max_size); | ||
| if (buf == nullptr) { | ||
| throw std::runtime_error(format("unable to allocate %s buffer", ggml_backend_buft_name(buft))); | ||
| } | ||
| bufs.emplace_back(buf); | ||
| buf_map[idx].push_back(buf); | ||
| } | ||
| bufs.emplace_back(buf); | ||
| buf_map.emplace(idx, buf); | ||
| } | ||
| } else { | ||
| ggml_backend_buffer_t buf; | ||
|
|
@@ -1773,7 +1780,7 @@ bool llama_model_base::load_tensors(llama_model_loader & ml) { | |
| } | ||
| bufs.emplace_back(buf); | ||
| for (uint32_t idx = 0; idx < ml.files.size(); idx++) { | ||
| buf_map.emplace(idx, buf); | ||
| buf_map[idx].push_back(buf); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1820,7 +1827,13 @@ bool llama_model_base::load_tensors(llama_model_loader & ml) { | |
| if (!ml.use_mmap) { | ||
| std::stable_partition(ctx_buf_maps.begin(), ctx_buf_maps.end(), [](const auto & ctx_buf_map) { | ||
| const auto & buf_map = ctx_buf_map.second; | ||
| return !buf_map.empty() && !ggml_backend_buffer_is_host(buf_map.begin()->second); | ||
| // one entry is a run of buffers now, not a single buffer, and every | ||
| // buffer in a context comes from the same buft, so the first one | ||
| // answers the question for all of them | ||
|
Comment on lines
+1830
to
+1832
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Join this predicate explanation into one physical line; it hard-wraps a single sentence across lines 1830-1832, contrary to the repository's explicit rule against splitting sentences across lines. AGENTS.md reference: AGENTS.md:L81-L81 Useful? React with 👍 / 👎. |
||
| if (buf_map.empty() || buf_map.begin()->second.empty()) { | ||
| return false; | ||
| } | ||
| return !ggml_backend_buffer_is_host(buf_map.begin()->second.front()); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Join this declaration comment into one physical line; it splits a single sentence between lines 247-248, and the new explanation in
src/llama-model.cppdoes the same at lines 1730-1731, contrary to the repository's explicit comment-formatting rule.AGENTS.md reference: AGENTS.md:L81-L81
Useful? React with 👍 / 👎.