Skip to content

Qualcomm AI Engine Direct - [LLM QAT] LLM Quant-Aware Distillation (QAD)#21036

Open
DannyYuyang-quic wants to merge 1 commit into
pytorch:mainfrom
CodeLinaro:dev1/danny/llm_QAT_QAD
Open

Qualcomm AI Engine Direct - [LLM QAT] LLM Quant-Aware Distillation (QAD)#21036
DannyYuyang-quic wants to merge 1 commit into
pytorch:mainfrom
CodeLinaro:dev1/danny/llm_QAT_QAD

Conversation

@DannyYuyang-quic

@DannyYuyang-quic DannyYuyang-quic commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Training:

  • Add BaseTrainer, Trainer (CE), KDTrainer (knowledge distillation)
  • Add CrossEntropyLoss, KLDivergenceLoss, linear warm-up cosine LR scheduler
  • Add TrainingArgs dataclass (epochs, lr, alpha, temperature, grad_accum_steps, warmup_ratio, lr_config)

Data pipeline:

  • Add DecoderDatasetBuilder.from_hf_source for HuggingFace SFT chat datasets
  • Add build_qat_dataloaders: explicit calib/train split
  • Add LLMTrainingCollator, make_causal_labels, make_conversation_labels
  • Add DataConfig train fields: train_tasks, train_hf_dataset, train_hf_limit

Quantization Strategy:

  • Add QATStrategy: PTQ calibration pass followed by KDTrainer/Trainer fine-tuning
  • Branch TextDecoder.quantize on --qat: prepare_qat_pt2e + move_exported_model_to_train vs prepare_pt2e
  • Select qat_recipe over quant_recipe when --qat is active

Quant recipe:

  • Add StaticLLMQATRecipe base class as a example
  • Add Smollm2QATQuantRecipe; add qat_recipe field to LLMModelConfig
  • Add 16a8w QAT qconfig

Fix:

  • SeqMSE: unwrap FakeQuantize wrapper before extracting observer

CI Testing:

  • Add test_static_llm_qat: assert QAT PPL < PTQ PPL on smollm2_135m

README:

  • examples/qualcomm/oss_scripts/llama/quantization_guidance.md

E2E script:

python examples/qualcomm/oss_scripts/llama/llama.py --build_folder build-android --device ${SERIAL_NUM} --soc_model ${SOC_MODEL} --decoder_model smollm2_135m --model_mode hybrid --max_seq_len 1024 --prompt "What dose it mean to edit written content?" --qat --train_hf_dataset "HuggingFaceTB/smol-smoltalk" --train_hf_limit 1000 --calib_hf_dataset "HuggingFaceTB/smol-smoltalk" --calib_hf_limit 1000

Result

I 00:00:00.284922 executorch:runner.cpp:228] Reading metadata from model
I 00:00:00.285219 executorch:runner.cpp:360] creating io_memory
I 00:00:00.285856 executorch:prompt_processor.cpp:273] Prompt Processor: total 18 prompt tokens (AR-32 * 1 iters)
I 00:00:00.295011 executorch:runner.cpp:473] RSS after prompt prefill: 181.175781 MiB (0 if unsupported)
I 00:00:01.262212 executorch:token_generator.cpp:347]
Reached to the end of generation
I 00:00:01.262249 executorch:runner.cpp:490] RSS after finishing text generation: 181.175781 MiB (0 if unsupported)
I 00:00:01.262275 executorch:stats.h:161]       Prompt Tokens: 18    Generated Tokens: 355
I 00:00:01.262284 executorch:stats.h:167]       Model Load Time:                0.284000 (seconds)
I 00:00:01.262291 executorch:stats.h:176]       Total inference time:           0.977000 (seconds)               Rate:  363.357216 (tokens/second)
I 00:00:01.262298 executorch:stats.h:185]               Prompt evaluation:      0.010000 (seconds)               Rate:  1800.000000 (tokens/second)
I 00:00:01.262305 executorch:stats.h:197]               Generated 355 tokens:   0.967000 (seconds)               Rate:  367.114788 (tokens/second)
I 00:00:01.262311 executorch:stats.h:205]       Time to first generated token:  0.010000 (seconds)
I 00:00:01.262318 executorch:stats.h:212]       Sampling time over 373 tokens:  0.112000 (seconds)
[INFO] [Qnn ExecuTorch]: Destroy Qnn context
[INFO] [Qnn ExecuTorch]: Destroy Qnn device
[INFO] [Qnn ExecuTorch]: Destroy Qnn backend

PyTorchObserver {"prefill_token_per_sec":1800,"decode_token_per_sec":367.115,"prompt_tokens":18,"generated_tokens":355,"model_load_start_ms":1753685358963,"model_load_end_ms":1753685359247,"inference_start_ms":1753685359247,"inference_end_ms":1753685360224,"prompt_eval_end_ms":1753685359257,"first_token_ms":1753685359257,"aggregate_sampling_time_ms":112,"SCALING_FACTOR_UNITS_PER_SECOND":1000}
/data/local/tmp/yuyazhua/executorch/static_llm/outputs/outputs.txt: 1 file pulled, 0 skipped. 1.8 MB/s (1714 bytes in 0.001s)
/data/local/tmp/yuyazhua/executorch/static_llm/outputs/inference_speed.txt: 1 file pulled, 0 skipped. 0.0 MB/s (7 bytes in 0.001s)
[INFO 2026-07-20 12:10:52,191 llama.py:290] Device Inference Results[0]:
<|im_start|>user
What dose it mean to edit written content?<|im_end|>
<|im_start|>assistant

1. **Editing for clarity**: When you edit your writing, you aim to make it clear and concise, ensuring that your message is easy for readers to understand and retain. This involves cutting unnecessary words, phrases, or sentences, and rephrasing or reorganizing your content to make it more readable.

2. **Editing for grammar**: Grammar is another crucial aspect of writing. When you edit your writing, you check for errors in grammar, syntax, and punctuation, which can make your writing more polished and error-free.

3. **Editing for style**: The final stage of editing is where you refine your writing to make it more engaging, clear, and engaging. This involves using language that is engaging, yet clear, and engaging in the way you want it to be.

4. **Editing for impact**: The final stage of editing is where you aim to make your writing more impactful. This means that you strive to make your message clear, concise, and impactful, and to convey it in a way that resonates with your readers.

5. **Editing for consistency**: When you edit your writing, you ensure that it is consistent in terms of tone, style, and voice. This involves using the same language, vocabulary, and structure to create a consistent tone, which is essential for effective communication.

To get started, take a step back and look at your writing. Read it aloud, if possible, to get a sense of the tone, style, and voice you're using. Then, read your writing aloud to get a sense of the rhythm, cadence, and flow.

When you're on the right track, you can begin to refine your writing and make it more engaging and effective.<|im_end|>

Test plan

python backends/qualcomm/tests/test_qnn_delegate.py TestExampleLLMScript.test_static_llm_qat --device ${SERIAL_NUM} --soc_model ${SOC_MODEL} -r . -a . --build_folder build-android

cc: @shewu-quic @haowhsu-quic @winskuo-quic @psiddh @abhinaykukkadapu

…for LLMs

Training:
- Add BaseTrainer, Trainer (CE), KDTrainer (knowledge distillation)
- Add CrossEntropyLoss, KLDivergenceLoss, linear warm-up cosine LR scheduler
- Add TrainingArgs dataclass (epochs, lr, alpha, temperature, grad_accum_steps, warmup_ratio, lr_config)

Data pipeline:
- Add DecoderDatasetBuilder.from_hf_source for HuggingFace SFT chat datasets
- Add build_qat_dataloaders: explicit calib/train split
- Add LLMTrainingCollator, make_causal_labels, make_conversation_labels
- Add DataConfig train fields: train_tasks, train_hf_dataset, train_hf_limit

Quantization Strategy:
- Add QATStrategy: PTQ calibration pass followed by KDTrainer/Trainer fine-tuning
- Branch TextDecoder.quantize on --qat: prepare_qat_pt2e +
  move_exported_model_to_train vs prepare_pt2e
- Select qat_recipe over quant_recipe when --qat is active

Quant recipe:
- Add StaticLLMQATRecipe base class as a example
- Add Smollm2QATQuantRecipe; add qat_recipe field to LLMModelConfig
- Add 16a8w QAT qconfig

Fix:
- SeqMSE: unwrap FakeQuantize wrapper before extracting observer

Testing:
- Add test_static_llm_qat: assert QAT PPL < PTQ PPL on smollm2_135m
@pytorch-bot

pytorch-bot Bot commented Jul 20, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/21036

Note: Links to docs will display an error until the docs builds have been completed.

⚠️ 16 Awaiting Approval

As of commit 2587988 with merge base 710edda (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 20, 2026
@DannyYuyang-quic

DannyYuyang-quic commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

@psiddh Hi,
As more users experiment with lower-bit quantization, maintaining model quality with PTQ alone becomes increasingly challenging. To improve output quality under more aggressive quantization settings, we've explored and added a simple end-to-end QAT (Quantization-Aware Training) flow for LLMs.

With QAT, we can further push quantization down to W4 PCQ, and potentially even lower precisions (W2). Below is a comparison between PTQ and QAT on SmolLM2-135M-instruct using W4 per-channel quantization for all decoder layers.

Please have a look, thanks!

Experiment Results: W4 Decoder PCQ (QAT vs PTQ)

QAT (W4 PCQ decoder)

python examples/qualcomm/oss_scripts/llama/llama.py --build_folder build-android --device ${SERIAL_NUM} --soc_model ${SOC_MODEL} --decoder_model smollm2_135m --model_mode hybrid --max_seq_len 1024 --prompt "What dose it mean to edit written content?" --qat --train_hf_dataset "HuggingFaceTB/smol-smoltalk" --train_hf_limit 1000 --calib_hf_dataset "HuggingFaceTB/smol-smoltalk" --calib_hf_limit 1000

Result

[INFO 2026-07-20 12:10:52,191 llama.py:290] Device Inference Results[0]:
<|im_start|>user
What dose it mean to edit written content?<|im_end|>
<|im_start|>assistant

1. **Editing for clarity**: When you edit your writing, you aim to make it clear and concise, ensuring that your message is easy for readers to understand and retain. This involves cutting unnecessary words, phrases, or sentences, and rephrasing or reorganizing your content to make it more readable.

2. **Editing for grammar**: Grammar is another crucial aspect of writing. When you edit your writing, you check for errors in grammar, syntax, and punctuation, which can make your writing more polished and error-free.

3. **Editing for style**: The final stage of editing is where you refine your writing to make it more engaging, clear, and engaging. This involves using language that is engaging, yet clear, and engaging in the way you want it to be.

4. **Editing for impact**: The final stage of editing is where you aim to make your writing more impactful. This means that you strive to make your message clear, concise, and impactful, and to convey it in a way that resonates with your readers.

5. **Editing for consistency**: When you edit your writing, you ensure that it is consistent in terms of tone, style, and voice. This involves using the same language, vocabulary, and structure to create a consistent tone, which is essential for effective communication.

To get started, take a step back and look at your writing. Read it aloud, if possible, to get a sense of the tone, style, and voice you're using. Then, read your writing aloud to get a sense of the rhythm, cadence, and flow.

When you're on the right track, you can begin to refine your writing and make it more engaging and effective.<|im_end|>

PTQ (W4 PCQ decoder)

python examples/qualcomm/oss_scripts/llama/llama.py --build_folder build-android --device ${SERIAL_NUM} --soc_model ${SOC_MODEL} --decoder_model smollm2_135m --model_mode hybrid --max_seq_len 1024 --prompt "What dose it mean to edit written content?" --calib_hf_dataset "HuggingFaceTB/smol-smoltalk" --calib_hf_limit 1000

Result

[INFO 2026-07-20 15:12:57,609 llama.py:290] Device Inference Results[0]:
<|im_start|>user
What dose it mean to edit written content?<|im_end|>
<|im_start|>assistant

1. A sentence is usually written in the first person, i.e., "He said, she said" or "She said, she said."

2. A paragraph is usually written in the third person, i.e., "She said, he said" or "She said, she said"

3. A couple is usually written in the second person, i.e., "She said, he said" or "He said, she said"

4. A little is usually written in the third person, i.e., "She said, he said" or "She said, she said"

5. A little is usually written in the second person, i.e., "She said, she said" or "She said, she said"

6. A little is usually written in the second person, i.e., "She said, she said" or "She said, she said"

7. A little is usually written in the second person, i.e., "She said, she said" or "She said, she said"

8. A little is usually written in the second person, i.e., "She said, she said" or "She said, she said"

9. A little is usually written in the second person, i.e., "She said, she said" or "She said, she said"

10. A little is usually written in the second person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 2nd person, i.e., "He said, she said" or "She said, she said"

Please note that the above examples are from the 2nd person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 1st person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 2nd person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 1st person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 2nd person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 1st person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 2nd person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 2nd person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 1st person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 2nd person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 1st person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 2nd person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 1st person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 2nd person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 2nd person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 1st person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 2nd person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 1st person, i.e., "She said, she said" or "She said, she said"

Please note that the above examples are from the 2nd person, i.e., "

@DannyYuyang-quic

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: qualcomm"

@pytorch-bot pytorch-bot Bot added the release notes: qualcomm Changes to the Qualcomm backend delegate label Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. release notes: qualcomm Changes to the Qualcomm backend delegate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant