-
Notifications
You must be signed in to change notification settings - Fork 391
Add imatrix support #633
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
Open
stduhpf
wants to merge
15
commits into
leejet:master
Choose a base branch
from
stduhpf:imatrix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add imatrix support #633
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
769d0ab
Imatrix: first implementation attempt
stduhpf ae2cbce
Refactor imatrix implementation into main example
stduhpf b5ecf2c
do not use logger in imatrix.hpp
stduhpf 134c235
imatrix: support DiT text encoders
stduhpf a6089d5
Model: merge split models when converting
stduhpf f719cb9
Do not warn about imatrix requirement if imatrix is provided
stduhpf ea6a543
Make imatrix not a header-only lib
stduhpf 7f9fd31
Warn user if imatrix will get overwritten
stduhpf 91a7a66
Fix missing includes
stduhpf ae0fcc2
Refactor imatrix api, fix build shared libs
stduhpf 67c30e1
imatrix: add docs
stduhpf 2dc5dfb
Avoid redefinition of ggml_log_callback_default
stduhpf 8c0b8e1
Fix typos
stduhpf 71eed14
forgot to use imatrix is some cases
stduhpf a386ba9
fix imatrix collection on CPU backend
stduhpf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Importance Matrix (imatrix) Quantization | ||
|
||
## What is an Importance Matrix? | ||
|
||
Quantization reduces the precision of a model's weights, decreasing its size and computational requirements. However, this can lead to a loss of quality. An importance matrix helps mitigate this by identifying which weights are *most* important for the model's performance. During quantization, these important weights are preserved with higher precision, while less important weights are quantized more aggressively. This allows for better overall quality at a given quantization level. | ||
|
||
This originates from work done with language models in [llama.cpp](https://github.com/ggml-org/llama.cpp/blob/master/examples/imatrix/README.md). | ||
|
||
## Usage | ||
|
||
The imatrix feature involves two main steps: *training* the matrix and *using* it during quantization. | ||
|
||
### Training the Importance Matrix | ||
|
||
To generate an imatrix, run stable-diffusion.cpp with the `--imat-out` flag, specifying the output filename. This process runs alongside normal image generation. | ||
|
||
```bash | ||
sd.exe [same exact parameters as normal generation] --imat-out imatrix.dat | ||
``` | ||
|
||
* **`[same exact parameters as normal generation]`**: Use the same command-line arguments you would normally use for image generation (e.g., prompt, dimensions, sampling method, etc.). | ||
* **`--imat-out imatrix.dat`**: Specifies the output file for the generated imatrix. | ||
|
||
You can generate multiple images at once using the `-b` flag to speed up the training process. | ||
|
||
### Continuing Training an Existing Matrix | ||
|
||
If you want to refine an existing imatrix, use the `--imat-in` flag *in addition* to `--imat-out`. This will load the existing matrix and continue training it. | ||
|
||
```bash | ||
sd.exe [same exact parameters as normal generation] --imat-out imatrix.dat --imat-in imatrix.dat | ||
``` | ||
With that, you can train and refine the imatrix while generating images like you'd normally do. | ||
|
||
### Using Multiple Matrices | ||
|
||
You can load and merge multiple imatrices together: | ||
|
||
```bash | ||
sd.exe [same exact parameters as normal generation] --imat-out imatrix.dat --imat-in imatrix.dat --imat-in imatrix2.dat | ||
``` | ||
|
||
### Quantizing with an Importance Matrix | ||
|
||
To quantize a model using a trained imatrix, use the `-M convert` option (or equivalent quantization command) and the `--imat-in` flag, specifying the imatrix file. | ||
|
||
```bash | ||
sd.exe -M convert [same exact parameters as normal quantization] --imat-in imatrix.dat | ||
``` | ||
|
||
* **`[same exact parameters as normal quantization]`**: Use the same command-line arguments you would normally use for quantization (e.g., target quantization method, input/output filenames). | ||
* **`--imat-in imatrix.dat`**: Specifies the imatrix file to use during quantization. You can specify multiple `--imat-in` flags to combine multiple matrices. | ||
|
||
## Important Considerations | ||
|
||
* The quality of the imatrix depends on the prompts and settings used during training. Use prompts and settings representative of the types of images you intend to generate for the best results. | ||
* Experiment with different training parameters (e.g., number of images, prompt variations) to optimize the imatrix for your specific use case. | ||
* The performance impact of training an imatrix during image generation or using an imatrix for quantization is negligible. | ||
* Using already quantized models to train the imatrix seems to be working fine. |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
both new options miss a new line.