Skip to content

quantize: Handle user-defined pruning of whole layers (blocks) #13037

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
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

EAddario
Copy link
Contributor

@EAddario EAddario commented Apr 20, 2025

This PR adds the ability to prune all tensors from user-defined layers (blocks) by providing a comma-separated list in the --prune-layers command line option. It will renumber remaining layers to avoid gaps in the sequence, update the relevant model metadata and, if an imatrix is available, it will use the correct importance score vector.

Pruning is restricted to repeating layers only (i.e. blk.n, blk.n+1, etc.) and will not affect single tensors like output, token_embd, etc.

This option can be used alongside --tensor-type to perform tensor/layer-wise quantization on selected tensor types, whilst at the same time pruning others. For example:

llama-quantize --tensor-type attn=q6_k --prune-layers 3,7,11 --imatrix imatrix.dat model-f32.gguf model-q4_k_m.gguf q4_k_m

It was inspired partly by ShortGPT: Layers in Large Language Models are More Redundant Than You Expect and partly as the next logical step from #12511. It could be used alongside #12718 to guide the layer selection.

Opening a draft PR for now until split tensor testing is completed, but feedback and suggestions are encouraged in the meantime.

@EAddario EAddario marked this pull request as draft April 20, 2025 21:47
@EAddario EAddario marked this pull request as ready for review June 14, 2025 12:56
@EAddario
Copy link
Contributor Author

EAddario commented Jun 14, 2025

Finally finished testing several combinations with split files and it seems to work 100%

Examples of pruned models here and here

@EAddario
Copy link
Contributor Author

Apologies for shotgun approach @slaren / @CISC / @ggerganov, not sure what the proper process to request a review is. Happy to close or move to draft if it's not suitable for merging

Comment on lines 670 to 675
if (false
|| it.first.find("attn_v.weight") != std::string::npos
|| it.first.find("attn_qkv.weight") != std::string::npos
|| it.first.find("attn_kv_b.weight")!= std::string::npos) {
pruned_attention_w++;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The false was added to get extra indent and alignment so that you could visually separate evaluation from statement, I think you failed (also missed minor whitespace adjustment). 😆

Comment on lines +630 to +634
if (!prune_list.empty()) {
uint32_t block_count = 0;
ml.get_key(LLM_KV_BLOCK_COUNT, block_count);
gguf_set_val_u32(ctx_out.get(), ml.llm_kv(LLM_KV_BLOCK_COUNT).c_str(), block_count - prune_list.size());
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to move this to after pruning has been done so that you know the actual size (in case user inputs non-existent layers)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants