Quantization optimization procedures - #1760
Conversation
|
@stduhpf you might want to take a look, I did some tests to determine the correct algorithm but the more testers the better :) |
wbruna
left a comment
There was a problem hiding this comment.
Maybe some of the details from the command-line usage and code comments could be moved to docs/quantization_and_gguf.md ?
|
Oh yes, definitely :) Everyone hates Claude's way of scattering random comment blobs around the code, don't know why they don't train it not to do that :/ |
Port of llama.cpp's auto-tensor-type tool (q3_pt branch) to diffusion models. During a normal generation run, the eval callback captures MUL_MAT input activations and reference outputs for representative weights of each (role, layer-bucket) class of the diffusion model; the analysis then quantizes each captured weight to every candidate type (imatrix-weighted, exactly as -M convert would), re-runs the matmuls on the captured activations, scores the relative-L2 output error, and solves an element-weighted multi-choice knapsack for the lowest-error type assignment within a bits-per-weight budget. The result is written as a ready-to-use --tensor-type-rules string. sd-cli: --att "out=rules.txt,bpw=3.5[,types=...|...][,buckets][,reps] [,samples][,stride][,max-tokens]"; combine with --imat-in, or --imat-out to collect the imatrix in the same run (the callback chains to the imatrix collector following the ask/collect protocol). Co-Authored-By: Claude Fable 5 <[email protected]>
It probably helps with performance. By writing the explaining comment before the actual code, the model is effectively prompting itself to wrtite the specific snippet. |
|
@stduhpf true, I didn't consider it might be a sort of "interleaved reasoning" :) |
Implement five follow-up ideas as --att toggles and adopt the one that helps: - sigma=uniform|low (default: low): ring-buffer capture of each weight's lowest-sigma (detail/glyph-formation) occurrences instead of sampling the whole denoise trajectory. A sigma x samples 2x2 factorial on Z-Image showed ~14% lower output error (mean rel-L2 / MSE-to-full-precision) vs uniform, consistent across Vulkan and ROCm backends and independent of sample count. - cost=mean|tail|maxmean (+ tail-q, tail-lambda): reduce per-capture error by a high quantile / mean+lambda*max of per-row rel-L2 instead of the mean. - role-floor= / floor-roles=: enforce a per-role minimum bpw (attention/adaLN). - topk=: emit the top-K distinct DP assignments, spread across the bpw band, plus a manifest, for end-to-end candidate validation. Only sigma=low changes a default; the other knobs are opt-in. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_011Lu4YuXcqKJL6Yvi86ybUW
Per review, strip the inline comments from src/runtime/auto_quant.{h,cpp} and
synthesize the design explanation (pipeline, scoring, DP, and the --att tuning
knobs) into docs/quantization_and_gguf.md instead.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011Lu4YuXcqKJL6Yvi86ybUW
Summary
This is a modified port of my tool I've been developing for mainline llama.cpp for smart quantizations.
Related Issue / Discussion
Basically, since we have imatrix for sd.cpp, this is a tool that utilizes the imatrix to create a custom quantization recipe that optimizes the quality of the resulting quant by upcasting the tensors that are critical for the quality and downcasting the ones that are less relevant. This allows to squeeze even more quality per bpw.