Skip to content

[DRAFT][quantization] Full quantization of LLama compatible models#436

Draft
stamalakhov wants to merge 1 commit intoSamsung:mainfrom
stamalakhov:full_quantization_br
Draft

[DRAFT][quantization] Full quantization of LLama compatible models#436
stamalakhov wants to merge 1 commit intoSamsung:mainfrom
stamalakhov:full_quantization_br

Conversation

@stamalakhov
Copy link
Contributor

@stamalakhov stamalakhov commented Jan 13, 2026

This draft tries to get fully quantized circle layers for Llama model.

TODO:

  • tests/cleanup

TICO-DCO-1.0-Signed-off-by: s.malakhov s.malakhov@partner.samsung.com

@stamalakhov stamalakhov self-assigned this Jan 13, 2026
@stamalakhov stamalakhov force-pushed the full_quantization_br branch 9 times, most recently from 0253cb9 to 5201525 Compare January 20, 2026 11:37
gptq[name] = GPTQ(subset[name])
gptq[name].quantizer.configure(
bits=8, perchannel=True, sym=False, mse=False
bits=4, perchannel=True, sym=False, mse=False
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI, you can give the option for this with this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI, you can give the option for this with this PR.

@mhs4670go
Thank you. I'll rebase after merging of #441.

Copy link
Contributor

@mhs4670go mhs4670go Jan 21, 2026

Choose a reason for hiding this comment

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

Could you give me some explanations for the reason of changes related with observers?

  1. Deleting some attributes and register them as buffer.
  2. Change ObserverBase's parent from ABC to torch.nn.Module. (and MinMaxObserver)

Copy link
Contributor Author

@stamalakhov stamalakhov Jan 21, 2026

Choose a reason for hiding this comment

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

Could you give me some explanations for the reason of changes related with observers?

  1. Deleting some attributes and register them as buffer.

Ahh. It occured that model.to("cuda") or model.to("cpu") do not transfer scales and zero_points to gpu/cpu, they were not registered as buffers or parameters, that is why they were registered as buffers. Deleting them is needed, because otherwise torch fails to register known attributes as buffers.

  1. Change ObserverBase's parent from ABC to torch.nn.Module

It will enable using buffer registering and correct automatical transfer of scales/zp to cpu<->gpu. The same approach is used in gptq/quant.py for the same reason (i suppose).
Please see

class Quantizer(nn.Module):

This draft is just PoC (quick and dirty).

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, thanks for the clarification. I'll reconsider those and apply the changes soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, thanks for the clarification. I'll reconsider those and apply the changes soon.

ok. Thank you very much.

continue
if (
dq.target
!= torch.ops.circle_custom.dequantize_mx_to_float.default
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems that just quantize_mx and dequantize_mx are simpler. Is there some consideration for exposing dtypes in the name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no fake_quantize for mx types (just circle_custom::quantize_mx). So quantize_float_to_mx is a try (m.b. failed) to distinguish it from quantize_mx. In case circle_custom::quantize_mx will become circle_custom::fakequantize_mx, then usual quantize/dequantize naming scheme applies.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mhs4670go
It can be renamed to any other (more appropriate) name.

Copy link
Contributor

@mhs4670go mhs4670go Jan 21, 2026

Choose a reason for hiding this comment

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

Ah, I see. How about go with quantize_mx_decomposed, and dequantize_mx_decomposed? This aligns with torch.ops.quantized_decomposed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I see. How about go with quantize_mx_decomposed, and dequantize_mx_decomposed? This aligns with torch.ops.quantized_decomposed.

@mhs4670go
Ok. Got it. Thank you.

@stamalakhov stamalakhov force-pushed the full_quantization_br branch 7 times, most recently from 60fcd6a to f7bb4d9 Compare January 27, 2026 13:51
@stamalakhov stamalakhov force-pushed the full_quantization_br branch 6 times, most recently from 06581cb to 542db37 Compare January 30, 2026 06:07
@stamalakhov stamalakhov changed the title [DRAFT][NO_MERGE][quantization] Full quantization [DRAFT][quantization] Full quantization Jan 30, 2026
@stamalakhov stamalakhov changed the title [DRAFT][quantization] Full quantization [DRAFT][quantization] Full quantization of LLama compatible models Feb 2, 2026
@stamalakhov stamalakhov force-pushed the full_quantization_br branch 2 times, most recently from 546d33e to f6d4a2b Compare February 2, 2026 11:51
@stamalakhov stamalakhov force-pushed the full_quantization_br branch 3 times, most recently from 02f9367 to ab81153 Compare February 4, 2026 09:20
@stamalakhov stamalakhov force-pushed the full_quantization_br branch 2 times, most recently from 628786e to 234d069 Compare February 5, 2026 06:56
try:
k_total, v_total = past_key_value.update(k_rot, v)
if len(sig.parameters) == 2:
k_total, v_total = past_key_value.update(k_rot, v)
Copy link
Contributor

Choose a reason for hiding this comment

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

@stamalakhov Just for curiosity, can this be exported? Or, it is just a fallback for float model?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep. it works. a lot of model outputs (which are just k, v outputs) for use_cache==True.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if len(sig.parameters) == 2 to make test passable

Copy link
Contributor

@mhs4670go mhs4670go Feb 5, 2026

Choose a reason for hiding this comment

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

When sig.parameters is 2? I'm asking this because I'm gonna trim the cache logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i've added if len(sig.parameters) == 2 to make quantization.wrapq.wrappers.llama.test_quant_attn.TestQuantLlamaAttention passable.
MockCache.update uses just two inputs.
For export of the whole LLama 3 inputs are required.

I'm gonna trim the cache logic.

@mhs4670go
You mean remove it completely?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I got it. I think you can just modify MockCache to have third parameter - layer_idx. Which will makes the codes simpler.

You mean remove it completely?

No. I just checked whether it has redundant logic. But, seems not that much:)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I got it. I think you can just modify MockCache to have third parameter - layer_idx. Which will makes the codes simpler.

@mhs4670go
Understood. I'll update MockCache. Thank you.

@stamalakhov stamalakhov force-pushed the full_quantization_br branch 6 times, most recently from fb0bd17 to 19cc002 Compare February 9, 2026 10:46
# Case A: HuggingFace-style transformers: model.model.layers
lm = getattr(root, "model", None)

embeddings = (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mhs4670go
May be it will be better to introduce something like QuantLlamaModel and wrap all internal structure (embed, lm_head, norm) inside of it? and leave this general code as a fallback? like this:

try:
    wrap(the_whole_model)
catch:
  no specific wrapper for a class then use  general logic

Copy link
Contributor

@mhs4670go mhs4670go Feb 10, 2026

Choose a reason for hiding this comment

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

It would be good to have QuantLlamaModel for convinience when we evaluate it or something like that. Just for note, even though the whole model is quantized, only decoder layers would need to be exported because of the runtime requirements. Nothing has been fixed though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. Got it. Thank you.

@stamalakhov stamalakhov force-pushed the full_quantization_br branch 3 times, most recently from 348cb52 to 83f9b1e Compare February 12, 2026 07:12
This draft tries to get fully quantized model.

TICO-DCO-1.0-Signed-off-by: s.malakhov <s.malakhov@partner.samsung.com>
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