-
Notifications
You must be signed in to change notification settings - Fork 12.2k
imatrix : use GGUF to store importance matrices #9400
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: master
Are you sure you want to change the base?
Conversation
* perplexity : simplify filling the batch
Sums and counts tensors no longer need to be consecutive. * imatrix : more sanity checks when loading multiple imatrix files * imatrix : use ggml_format_name instead of std::string concatenation Co-authored-by: Xuan Son Nguyen <son@huggingface.co>
I'm setting this to "draft", because of concerns by @ikawrakow in ikawrakow/ik_llama.cpp#15 (comment) and ikawrakow/ik_llama.cpp#15 (comment) (mostly related to the fact that GGUF is harder to parse than More details near the end of ikawrakow/ik_llama.cpp#15 (reply in thread). I'll need some days to think about how to go further with this. |
@compilade This is a good change and I think it would be useful to bring it to a completion. In the future, we can extend |
This should make comparisons between the formats easier because this matches the behavior of the previous version.
tools/imatrix/imatrix.cpp
Outdated
static bool str_remove_suffix(std::string & str, const std::string & suffix) { | ||
bool has_suffix = str_has_suffix(str, suffix); | ||
if (has_suffix) { | ||
str = str.substr(0, str.size() - suffix.size()); | ||
} | ||
return has_suffix; | ||
} |
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.
static bool str_remove_suffix(std::string & str, const std::string & suffix) { | |
bool has_suffix = str_has_suffix(str, suffix); | |
if (has_suffix) { | |
str = str.substr(0, str.size() - suffix.size()); | |
} | |
return has_suffix; | |
} | |
static bool str_remove_suffix(std::string & str, const std::string_view & suffix) { | |
bool has_suffix = string_ends_with(str, suffix); | |
if (has_suffix) { | |
str = str.substr(0, str.size() - suffix.size()); | |
} | |
return has_suffix; | |
} |
This is a nice complement to string_ends_with
, should be moved to common.cpp
as string_remove_suffix
.
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.
Shouldn't std::string_view
be passed by value instead of by const reference in these functions?
For example, https://quuxplusone.github.io/blog/2021/11/09/pass-string-view-by-value/ suggests that std::string_view
should always be passed by value.
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.
I did wonder about that, but didn't know enough of the details behind it, reading that it certainly seems so, and it should be worth going over the usage of std::string_view
elsewhere.
Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com>
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.
LGTM, I did some light testing with old and new formats, converting them back and forth, checking that quantization was unchanged, etc, all seems good.
Only slightly annoying thing is the garbage output to console (by Resolved in #14381gguf_init_from_file
) when using old format.
Will probably benefit from testing by more people before merging, @bartowski1182 @danielhanchen ?
Thank you for working on this, I've been thinking that storing imatrix as gguf wold be nice for investigating the use of gradients instead of activations. |
Follow-up from ikawrakow/ik_llama.cpp#15 (reply in thread).
Using GGUF as the format for
imatrix
files will be useful for further experiments (e.g. with L²QER) and compatibility with existing or future GGUF tooling (e.g. GGUF previews on HuggingFace, graphical GGUF viewer(s) #6715, some kind ofgguf-diff
, etc.).There are multiple problems with
imatrix
which this is addressing:unordered_map
iteration order (makessha256sum
useless to compareimatrix
files made on the same dataset)-ub
(intermediate saves happen waaay too often)Summary of changes
imatrix
data.general.type
isimatrix
general.architecture
imatrix
files.*.in_sum2
and*.counts
for each tensors with imatrix data.*.in_sum2
are the per-channel sums of squared activationsF32
, like before.*.counts
are the number of activations (also the number of tokens), useful to calculate the mean squared activations (which is used byllama-quantize
)imatrix
files together with--in-file
.F32
even though it's integer values, because when calculating the mean it would be converted toF32
anyway to perform the division.Addconvert_legacy_imatrix_to_gguf.py
to convert oldimatrix.dat
files toimatrix.gguf
llama-quantize
can still read the old format (with a warning)) or can be converted withllama-imatrix
directly (when the output file has the.gguf
suffix).llama-perplexity
since perplexity : support using multiple sequences to allow larger batch sizes #5946, allow computing multiple chunks per batch withllama-imatrix
Use fused-multiply-add (withstd::fma
) when accumulating the sums of activationsllama-imatrix
frommaster
)f64
would be even better, but I'm not use it's worth it yet. For the curious, usingdouble
for the intermediate accumulations can be tried by changing only one line inIMatrixStats
:vector<float> values
tovector<double> values
.)unordered_map
.sha256sum
can be meaningfully used to compareimatrix
files generated in very similar conditions.TODO
llama-quantize
with oldimatrix.dat
with newllama-quantize
using convertedimatrix.gguf
sha256sum
.llama-imatrix
at different batch sizes-ub 64 -b 512
and-ub 512 -b 2048
for a chunk size of 512 (-c 512
)llama-imatrix
vs newllama-imatrix
--in-file
withllama-imatrix
.imatrix
or.gguf
imatrix (for round-trip conversions)general.architecture
exclusion.self.add_architecture()
a no-op, but maybegeneral.architecture
should simply be excluded whenself.arch == ""
. Not sure how to prevent using the otherself.add_*
(inGGUFWriter
) which expectself.arch
to be something.imatrix.dat
files?