Skip to content

[quantization] Support multi-turn workflows#818

Merged
mhs4670go merged 3 commits into
Samsung:mainfrom
mhs4670go:app
Jul 20, 2026
Merged

[quantization] Support multi-turn workflows#818
mhs4670go merged 3 commits into
Samsung:mainfrom
mhs4670go:app

Conversation

@mhs4670go

@mhs4670go mhs4670go commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

This commit supports multi-turn workflows.

python -m tico.quantization.recipes.debug.static_llama_runtime \
  --model /mnt/sdb/model/Llama-3.2-1B-Instruct \
  --max-seq 256 \
  --device cpu \
  --no-run-baseline \
  --run-multiturn \
  --append-bucket 16


python -m tico.quantization.recipes.debug.static_llama_runtime \
  --model /mnt/sdb/model/Llama-3.2-1B-Instruct \
  --max-seq 256 \
  --padding-side right \
  --device cpu \
  --no-run-baseline \
  --run-multiturn \
  --multiturn-first-prompt $'User: What is the capital of France?\nAssistant:' \
  --multiturn-second-turn $'\nUser: What country is that city in?\nAssistant:' \
  --multiturn-decode-steps-before-append 4 \
  --multiturn-decode-steps-after-append 4 \
  --append-bucket 16 \
  --compare-append-prefill-with-decode-loop
...
====================================================================================================
Multi-turn post-append decode logits step 1
mean|diff| = 0.02939297
 max|diff| = 0.10098839
PEIR       = 0.340664 %
====================================================================================================
Multi-turn post-append decode logits step 2
mean|diff| = 0.03495336
 max|diff| = 0.17215163
PEIR       = 0.569002 %
====================================================================================================
Multi-turn post-append decode logits step 3
mean|diff| = 0.02905156
 max|diff| = 0.19826031
PEIR       = 0.583857 %
====================================================================================================
Multi-turn post-append decode logits step 4
mean|diff| = 0.01603191
 max|diff| = 0.09334183
PEIR       = 0.341133 %
====================================================================================================
Multi-turn generated text:
User: What is the capital of France?
Assistant: The capital of France
User: What country is that city in?
Assistant: France
User:

Related: #812
TICO-DCO-1.0-Signed-off-by: seongwoo mhs4670go@naver.com

@mhs4670go mhs4670go added the DRAFT label Jul 8, 2026
@mhs4670go mhs4670go changed the title [DRAFT] Support multi-turn workflows [quantization] Support multi-turn workflows Jul 14, 2026
@mhs4670go
mhs4670go marked this pull request as ready for review July 14, 2026 06:06
@mhs4670go mhs4670go removed the DRAFT label Jul 14, 2026
@mhs4670go
mhs4670go requested a review from Torrero July 14, 2026 07:05
return hidden, new_k, new_v


class LlamaAttentionAppendPrefillExportAdapter(nn.Module):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Couldn't find any references to this class.

return hidden, new_k, new_v


class LlamaDecoderLayerAppendPrefillExportAdapter(nn.Module):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This class looks absolutely identical to LlamaDecoderLayerDecodeExportAdapter. Can we simply reuse the latter one?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I'll remove the duplicate class and reuse it while keeping append_prefill as a separate export mode.

Return contract
---------------
- return_kv=True:
(hidden_states, (new_key, new_value))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The actual code returns a flat tuple (hidden_states, new_key, new_value), not a nested tuple.

@dvsav

dvsav commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

(Optional) Add unit tests

The PR adds a lot of new functionality (new export mode, new adapters, multi-turn runtime with attention mask construction, cache management, chunked append) but includes no unit tests. The only validation is the debug script static_llama_runtime.py. Key areas needing tests:

  • _build_append_prefill_attention_mask correctness (causal within new block, masking of gap, padded rows).
  • append_prefill cache write-back correctness.
  • append_prefill_chunked with chunks smaller than bucket size.
  • Export of append-prefill graphs at various bucket sizes.

def __init__(self, wrapped, *, return_kv: bool = True):
super().__init__()
self.wrapped = wrapped
self.wrapped.return_type = "tuple"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(Optional) return_type = "tuple" mutation in __init__ is fragile

All three decoder-layer adapters set self.wrapped.return_type = "tuple" in __init__. Since the runtime creates all three adapters from the same wrapped module, the last __init__ wins. This works today because all set the same value, but it's a latent bug if a future adapter sets a different return_type. Consider setting return_type in forward() or making it a parameter of the adapter rather than mutating the wrapped module.

@dvsav

dvsav commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

(Optional) Implement bucket selection logic

The issue specification describes runtime bucket selection: "The runtime selects the smallest usable bucket satisfying: T <= Q and P + Q <= K." The current append_prefill_chunked uses a fixed bucket_size for all chunks. A production runtime should select the smallest bucket that fits each chunk, and handle the case where no bucket fits (context-length policy). The current implementation (_normalize_append_prefill_buckets) raises ValueError when capacity is exceeded, which is acceptable for a first version but should be documented as a limitation.

@mhs4670go

Copy link
Copy Markdown
Contributor Author

@dvsav PTAL:)

dvsav
dvsav previously approved these changes Jul 16, 2026

@dvsav dvsav left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 LGTM. Thank you!

Torrero
Torrero previously approved these changes Jul 16, 2026

@Torrero Torrero left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

This commit supports multi-turn workflows.

TICO-DCO-1.0-Signed-off-by: seongwoo <mhs4670go@naver.com>
Document that the decoder-layer adapter supports both single-token decode and multi-token append-prefill shapes.

TICO-DCO-1.0-Signed-off-by: seongwoo <mhs4670go@naver.com>
@mhs4670go

Copy link
Copy Markdown
Contributor Author

@dvsav @Torrero I've just rebased the PR. PTAL

@dvsav dvsav left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

@mhs4670go
mhs4670go merged commit 312ed89 into Samsung:main Jul 20, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants