[test+operators] Fuse llama attention to circle attention#217
Closed
glistening wants to merge 21 commits intoSamsung:mainfrom
Closed
[test+operators] Fuse llama attention to circle attention#217glistening wants to merge 21 commits intoSamsung:mainfrom
glistening wants to merge 21 commits intoSamsung:mainfrom
Conversation
a255e2f to
2cf5d50
Compare
Contributor
Author
Contributor
Author
00596a2 to
4ef9092
Compare
glistening
commented
Jul 23, 2025
Comment on lines
27
to
40
| from tico.utils.record_input import RecordingInput | ||
|
|
||
| # past_key_values | ||
| # --------------- | ||
| # During prefill, "past_key_values" not None, but an empty Cache instance. | ||
| # Passing None makes torch.export happy. | ||
|
|
||
| # attention_mask, cache_position | ||
| # ------------------------------ | ||
| # For npu, ignore captured values generated from example prompt. | ||
|
|
||
| input_to_remove = ["past_key_values", "attention_mask", "cache_position"] | ||
|
|
||
| with torch.no_grad(), RecordingInput(model, input_to_remove=input_to_remove) as rec: |
Contributor
Author
There was a problem hiding this comment.
The only change to capture input.
| do_sample=False, | ||
| pad_token_id=tokenizer.eos_token_id, | ||
| ) | ||
| captured_input = rec.captured_input |
Contributor
Author
There was a problem hiding this comment.
Retrieve captured_input
|
|
||
| model = AutoModelForCausalLM.from_pretrained(model_name) | ||
| model.eval() | ||
| circle_model = tico.convert(model, captured_input) |
Contributor
Author
There was a problem hiding this comment.
Then, pass captured_input to tico.convert.
Contributor
Author
7/28 Offline Code Review
|
Contributor
Author
|
I took a look into |
5a35218 to
2501221
Compare
glistening
commented
Aug 4, 2025
| return hidden_states | ||
|
|
||
|
|
||
| @torch.library.register_fake("circle::attention.llama") |
Contributor
Author
There was a problem hiding this comment.
Without this,
torch._dynamo.exc.Unsupported: Operator does not support running with fake tensors
Developer debug context: unsupported operator: circle.attention.llama
Contributor
Author
|
This was referenced Sep 11, 2025
c1a9ca9 to
375c62f
Compare
This was referenced Nov 4, 2025
added 19 commits
November 4, 2025 14:09
It introduces circle_attention op, and add tests which fuse attention from LlamaDecoderLayers. TICO-DCO-1.0-Signed-off-by: Sanggyu Lee <sg5.lee@samsung.com>
model.py for LlamaModel layer.py for LlamaDecoderLayer
library.implf for "CPU" turned out not necessary.
Causal attention_mask can be calculated in op_attention. However, op_attention is not the proper place because the cos and sin table needs to be calculated again and again in each attention layer while the cos and sin table is sharable between decode layers.
ae241d2 to
7ed0d2a
Compare
Contributor
Author
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.


It introduces circle_attention op, and add tests which fuse attention from LlamaDecoderLayers.
TICO-DCO-1.0-Signed-off-by: Sanggyu Lee sg5.lee@samsung.com
It is another fuser. It does not use pattern match, but it adds an operator using decorator.
To generate tinyllama circle, run:
python test/modules/model/LlamaDecoderLayerWithKVCache/layer.py # 1 decoder-layer onlyor
python test/modules/model/LlamaDecoderLayerWithKVCache/layers.py # All decoder-layers