Quantization optimization procedures#1760
Open
pwilkin wants to merge 3 commits into
Open
Conversation
Contributor
Author
|
@stduhpf you might want to take a look, I did some tests to determine the correct algorithm but the more testers the better :) |
wbruna
reviewed
Jul 7, 2026
wbruna
left a comment
Contributor
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 ?
Contributor
Author
|
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 <noreply@anthropic.com>
Contributor
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. |
Contributor
Author
|
@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) <noreply@anthropic.com> 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) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011Lu4YuXcqKJL6Yvi86ybUW
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.