Skip to content

Refactor/multiswitch only and conversation ids - #131

Merged
yairallouche merged 17 commits into
mainfrom
refactor/multiswitch-only-and-conversation-ids
Sep 10, 2026
Merged

Refactor/multiswitch only and conversation ids#131
yairallouche merged 17 commits into
mainfrom
refactor/multiswitch-only-and-conversation-ids

Conversation

@noaakl

@noaakl noaakl commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

The doc was a standalone styled HTML page, which meant it rendered as raw
markup on GitHub and sat outside the pre-commit link validator: that hook
only reads tracked .md, .ipynb and .py files, and .html is absent from its
EXT_OK set, so every link pointing AT the page was unchecked too. As
Markdown it renders in-place and its inbound links are now validated.

Structure is preserved: all 10 sections, 18 tables, and every ASCII
diagram and verbatim trace. Typographic punctuation in prose is ASCII
(the box-drawing characters in the diagrams are kept, being load-bearing);
the table of contents is now anchor-linked.

Pointers repointed at the new extension:
  CLAUDE.md                                    gotcha 10
  src/granite_switch/vllm/switch/multi.py:361  the 188-bound comment
  tutorials/notebooks/hello_multiswitch.ipynb
  tutorials/notebooks/multi_turn_multiswitch.ipynb
  tutorials/notebooks/multiswitch_serving.ipynb

Content corrections made while converting:

- switch_type's default is "multi" as of the next commit on this branch,
  so the comparison table and the config-surface table say so. Added a
  short subsection explaining why the two create_switch() call sites keep
  a "single" getattr fallback: it only fires for a config.json predating
  c5e78c6, whose num_hidden_layers was inflated by 1 rather than 2.

- Stale line references corrected against this branch: config.py rows in
  the config-surface table, modeling_granite_switch.py:214-216 ->
  :343-344, conversation.py:99 -> :110.

Remaining line references are carried over from the HTML and were not
re-audited; the doc header now says so rather than implying otherwise.

Signed-off-by: noaa <noaa.kless@ibm.com>
…t_payload

Both policies reuse previously-sent ids as a stable prefix, so both must
travel as ids. requires_token_ids is now constant True and the chat_payload
method is removed (it returned a messages body the chat endpoint re-renders
server-side, unable to carry ids). The /v1/chat/completions endpoint itself
is unaffected; callers that want it just do not route through Conversation.

conversation.py also carries the branch's SingleSwitch-removal cleanup of the
old switch_type='multi' guard.

Signed-off-by: noaa <noaa.kless@ibm.com>
Adds _appendable (control-token position decides whether the new turn can be
a delta) and _control_char_index. A turn whose control token lands inside the
already-sent prefix -- a LoRA token at index 0, or an aLoRA invocation in an
earlier message -- can no longer be a delta; instead of raising, build_prompt
re-prefills (full render, history's control tokens dropped). _reprefill is
generalized with a reason so budget and placement fallbacks log accurately.

A genuinely non-append-only template (Granite 4.2 thinking truncation) still
raises: the discriminator is whether a no-adapter render extends the prefix.
_reject_lora_placement is removed; _explain_no_prefix reduced to the template
case. No adapter-technology detection anywhere.

Signed-off-by: noaa <noaa.kless@ibm.com>
RE_PREFILL now carries _re_base_ids/_re_base_text: the base-rendered history
prefix already sent (control tokens dropped), reused verbatim next turn so the
prefix cache hits. build_prompt renders history with adapter=None (answers are
already control-token-free, so this is base by construction) and appends the
current turn's delta; a turn's base form is only sent the turn after it, so
reuse lags one turn. Turn 1, a LoRA turn, or a non-append-only template
full-renders. Output is byte-identical to a from-scratch RE_PREFILL render --
reuse is a cache optimization, never a change to what the model sees.

Signed-off-by: noaa <noaa.kless@ibm.com>
Confirms the bf16 counting-head budget guard is PRESERVE-only: RE_PREFILL
demotes history to base, so 50 turns each carry at most the current turn's one
control token and never approach MAX_RETAINED_CONTROL_TOKENS. (The PRESERVE
unappendable->reprefill wiring landed with Task 2.)

Signed-off-by: noaa <noaa.kless@ibm.com>
RE_PREFILL now reuses base-demoted ids (lag one turn); an unappendable turn
(LoRA at position 0, or an aLoRA trigger in an earlier turn) re-prefills
instead of raising; chat_payload is removed and requires_token_ids is always
true. Rewrites the two-policies table, the delta logic, the API surface, the
refusals->fallback section, and recomputes the PRESERVE matrix with exact
CPU-verified id counts. The one remaining raise is a non-append-only template.
Test-inventory counts refreshed.

Signed-off-by: noaa <noaa.kless@ibm.com>
Section 8 rewritten: PRESERVE no longer refuses a LoRA or stale-invocation
turn, it re-prefills (demos now assert reprefills increments rather than a
raise); chat_payload and the switch_type='single' demo are gone; the one
remaining constructor raise is config-omitted, and the one build_prompt raise
is a non-append-only template. Removes the dropped --switch-type CLI flag and
the switch_type persistence assertion from section 9 (SingleSwitch is gone),
and updates the intro, RE_PREFILL id-reuse framing, and the 188-ceiling note
(re-prefill, not raise). Reprefill claims are backed by the unit tests.

Signed-off-by: noaa <noaa.kless@ibm.com>
SingleSwitch is deleted from src, but the doc still described it as a live
engine. create_switch no longer dispatches (it returns MultiSwitch directly);
switch_type is no longer a config parameter but a rejection marker in
from_dict. Rewrites the create_switch snippet, the switch_type-default table
(now the rejection behavior), the comparison table (SingleSwitch marked
removed, deleted-file reference dropped), the config-surface table (switch_type
row removed), the layer-offset comment, and the base-reset row (dropped the
removed validate_base_reset_switch_type). Also fixes the section-6 delta
pseudocode: LoRA/earlier-trigger re-prefills, only a non-append-only template
raises. Test reference updated to test_single_switch_rejected.py.

Signed-off-by: noaa <noaa.kless@ibm.com>
SingleSwitch's single base->adapter transition (±gain cumsum over one
attention head) is superseded by MultiSwitch's Kerdock/DG coded memory,
which routes arbitrarily many transitions per request, latest-wins, and
supports return-to-base. SingleSwitch averaged competing control tokens
and mis-routed, and had no mechanism to re-select base mid-sequence.

Delete both backends' single.py and all SingleSwitch-only tests and
helpers (test_single_switch*, single_switch_cases, sequences,
test_sharpness_equivalence; test_token_exchange's vLLM copy). create_switch
builds MultiSwitch directly (no engine dispatch); from_dict rejects
SingleSwitch/legacy checkpoints (pinned by test_single_switch_rejected.py);
MultiSwitch owns num_cache_layers == 2 (counting + memory), where
SingleSwitch owned 1.

Signed-off-by: noaa <noaa.kless@ibm.com>
Four tests were added or shaped on main after this branch forked, while
SingleSwitch was still the default engine. Removing SingleSwitch changes what
they exercise, so update them to the MultiSwitch-only world:

- test_control_lut_refresh.py / test_token_exchange.py: drop the ``"single"``
  arm from the LUT-refresh parametrization; create_switch only ever returns
  MultiSwitch now, so the single arm asserted a type that can no longer be built.
- test_granitemoe_compose_e2e.py: the switch reserves SWITCH_CACHE_LAYERS (== 2,
  MultiSwitch's counting + memory slots) at the front, not 1. Assert
  ``NUM_LAYERS + SWITCH_CACHE_LAYERS``; the old ``+ 1`` was the SingleSwitch
  single-slot layout.
- test_multi_audio_compose_e2e.py: drop the ``--switch-type`` CLI flag (removed
  with SingleSwitch) and the ``single`` compose leg, and stop asserting a
  ``switch_type`` config key the composer no longer writes.

The behavior each test pins (LUT refresh, front-loaded cache slots, audio compose
shipping a consistent control LUT) is unchanged.

Signed-off-by: noaa <noaa.kless@ibm.com>
The Vela run surfaced ~34 GPU/E2E failures the CPU suite never exercised, all
from SingleSwitch assumptions left in tests after the engine was removed:

- MultiSwitch reserves 2 cache slots (counting + memory), not 1. Fix the stale
  `num_hidden_layers - 1` in _model_forward_tests.py (its sibling at line 384 was
  already -2) and rewrite the KV-cache setup to register BOTH switch attention
  modules (counting_attn/memory_attn) instead of a single `.attn`. Size
  test_sr_switch's config to 4 layers (2 switch + 2 decoder) and drop the inert
  switch_type="single".
- `single_overrides` was removed from generation_models; _noneager_generation_tests
  now uses basic_overrides (== switch_overrides, MultiSwitch).
- Drop `config.switch_type` reads (the attribute no longer exists): the redundant
  asserts in test_multi_switch_alora/mixed_tech (both already isinstance-check
  MultiSwitch), the switch_type field emitted by the conversation workers, and the
  asserts consuming it. Build-phase anti-vacuity checks that read the raw config
  now key off the durable `ms_code_m` marker instead.

Verified: test_sr_switch passes on CPU (25); all edited files clean under ruff 0.9.0
lint + format. The vLLM/integration ones re-verify on the next Vela run.

Signed-off-by: noaa <noaa.kless@ibm.com>
…ingleSwitch preview

test_quantization loaded ibm-granite/granite-switch-4.1-3b-preview, which the
branch's from_dict now correctly rejects: that published checkpoint (like the 8b
and 30b previews) is a legacy pre-coded-engine build — no ms_code_m, no
switch_type — so it cannot load as MultiSwitch. No MultiSwitch checkpoint is
published anywhere on the Hub.

Follow the same pattern as test_multi_switch_mixed_tech: gate on
GRANITE_SWITCH_E2E_MODELS=1 and compose (warm-reuse) a real ~3B mixed checkpoint
from rag + guardian under GRANITE_SWITCH_E2E_DIR/multi-mixed. That yields exactly
the two adapters ADAPTER_TESTS exercises — answerability (aLoRA, from rag) and
hallucination_detection (LoRA, from guardian) — and reuses the identical checkpoint
mixed_tech already composes, so no extra compose cost on a full run.

GPU/E2E-only; verifies on the next Vela run.

Signed-off-by: noaa <noaa.kless@ibm.com>
…h preview

test_pipeline_parallelism_equivalence hardcoded ibm-granite/granite-switch-4.1-3b-preview,
the same legacy SingleSwitch checkpoint from_dict now rejects — so both PP=1 and
PP=2 worker steps died at config load. PP only needs a loadable MultiSwitch
checkpoint with an active adapter to compare PP=1 vs PP=2 token equivalence, so
gate on GRANITE_SWITCH_E2E_MODELS=1 and compose (warm-reuse) the same multi-mixed
checkpoint test_quantization and test_multi_switch_mixed_tech build.

GPU/E2E-only (needs 2 GPUs); verifies on the next Vela run.

Signed-off-by: noaa <noaa.kless@ibm.com>
…ed adapters

A GPU probe showed the 8 test_adapter_activates failures were neither a
quantization nor a switch bug: the checkpoint the fixture composed did not contain
answerability or hallucination_detection at all (its adapter_names were guardian-
family: factuality-correction/-detection, guardian-core, policy-guardrails). So
apply_chat_template(adapter_name="answerability") matched nothing, the control
token was never injected (base and adapter prompts identical), and the adapter was
a no-op in both bf16 and 4-bit — base == adapter, the assertion fails.

granitelib-rag carries BOTH tested adapters (answerability as aLoRA,
hallucination_detection as LoRA), so compose from rag alone into a dedicated
"quant-rag" dir. The dedicated dir matters: the shared "multi-mixed" path may
already hold a checkpoint composed from other libraries whose names would not
match ADAPTER_TESTS.

GPU/E2E-only; verifies on the next Vela run.

Signed-off-by: noaa <noaa.kless@ibm.com>
….to()

Removing SingleSwitch makes MultiSwitch the only (and default) engine, so every
composed checkpoint now carries the fp32 Kerdock codebook buffer. The composer
casts the model with model.to(bfloat16) (compose_utils.py), which nn.Module._apply
would take the codebook down with it — but __init__ rebuilds the buffer as fp32 on
every load. A cast model then writes a bf16 codebook while a reload of it writes
fp32, so the same model serializes to two different byte counts (a 262,144-byte
gap: 2048x64 fp32 vs bf16).

Masked before the SingleSwitch removal because compose defaulted to the
codebook-free SingleSwitch, so test_save_load_compose.py's byte-idempotency check
(TestPhase2_DoubleSerialization::test_file_content_matches) never exercised the
codebook path. With MultiSwitch as the default it does, and fails. _apply upcasts
the codebook back to fp32 after any cast, restoring idempotency; the forward path
is unaffected (codebook entries +/-0.125 are exact in bf16 and feed an fp32 dest).

Verified: test_file_content_matches passes on CPU; tests/hf/test_multi_switch.py 297 pass.
Signed-off-by: noaa <noaa.kless@ibm.com>
…rity

Five branch-touched files had formatting the pinned ruff (0.9.0) reformats, and
the aligned multi_turn_multiswitch notebook carried cell outputs. Apply
ruff-format and nbstripout so `pre-commit run --all-files` (the CI source of
truth) is clean.

Signed-off-by: noaa <noaa.kless@ibm.com>
Remove session/branch cruft that does not help a reader understand the code:

- MULTISWITCH_EXPLAINED.md: drop the "Converted from HTML on branch ... read at
  1c86ee9 ... not been re-audited" provenance note, the "on this branch" framing,
  and the commit/branch archaeology for the alora-invocation-tail rule (kept the
  fact: the code uses the character rule, no alora_invocation_tail() helper here).
- Drop "N bugs fixed on this branch" narrative from the multiswitch test
  docstrings, keeping the timeless rationale (each guards an end-to-end boundary).
- refresh_switch_control_lut: drop the removed-SingleSwitch / --switch-type history,
  keep why it is extracted (testable without a real compose).
- Retarget dangling references to the deleted SingleSwitch test
  test_switch_e2e_compose.py to the MultiSwitch serving e2e tests / generic notes.

Signed-off-by: noaa <noaa.kless@ibm.com>
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.55072% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/granite_switch/conversation.py 98.24% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@aviv1ron1

Copy link
Copy Markdown
Collaborator

/gpu-test-multi

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

✅ GPU tests passed — vllm19-multi

2852 passed, 66 skipped, 22 warnings in 11068.91s (3:04:28)

Commit: d434cc524b890121b3c4a92d7edcd11184802ac0
Full run & artifact log

Last 40 log lines

.venv/lib/python3.12/site-packages/torch/jit/_script.py:362: 14 warnings
  /tmp/granite-switch/.venv/lib/python3.12/site-packages/torch/jit/_script.py:362: DeprecationWarning: `torch.jit.script_method` is deprecated. Please switch to `torch.compile` or `torch.export`.
    warnings.warn(

tests/composer/test_compose_e2e.py:83
  /tmp/granite-switch/tests/composer/test_compose_e2e.py:83: PytestUnknownMarkWarning: Unknown pytest.mark.xdist_group - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.xdist_group("compose_e2e")

tests/composer/test_multi_audio_compose_e2e.py:124
  /tmp/granite-switch/tests/composer/test_multi_audio_compose_e2e.py:124: PytestUnknownMarkWarning: Unknown pytest.mark.xdist_group - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.xdist_group("multi_audio_compose_e2e")

tests/composer/test_multi_audio_compose_e2e.py:130
  /tmp/granite-switch/tests/composer/test_multi_audio_compose_e2e.py:130: PytestUnknownMarkWarning: Unknown pytest.mark.xdist_group - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.xdist_group("multi_audio_compose_e2e")

tests/composer/test_upstream_files.py:218
  /tmp/granite-switch/tests/composer/test_upstream_files.py:218: PytestUnknownMarkWarning: Unknown pytest.mark.xdist_group - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.xdist_group("upstream_build_e2e")

tests/integration/test_hf_to_vllm_weights.py::TestMultiSwitchForwardEquivalence::test_forward_logit_equivalence
  /tmp/granite-switch/.venv/lib/python3.12/site-packages/flashinfer/gemm/kernels/grouped_gemm_masked_blackwell.py:2059: DeprecationWarning: tcgen05.OperandMajorMode is deprecated, use cute.nvgpu.OperandMajorMode instead
    a_major_mode: tcgen05.OperandMajorMode,

tests/integration/test_hf_to_vllm_weights.py::TestMultiSwitchForwardEquivalence::test_forward_logit_equivalence
  /tmp/granite-switch/.venv/lib/python3.12/site-packages/flashinfer/gemm/kernels/grouped_gemm_masked_blackwell.py:2061: DeprecationWarning: tcgen05.OperandMajorMode is deprecated, use cute.nvgpu.OperandMajorMode instead
    b_major_mode: tcgen05.OperandMajorMode,

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
========= 2852 passed, 66 skipped, 22 warnings in 11068.91s (3:04:28) ==========
sys:1: DeprecationWarning: builtin type swigvarlink has no __module__ attribute
[rank0]:[W909 16:34:15.132002371 ProcessGroupNCCL.cpp:1553] Warning: WARNING: destroy_process_group() was not called before program exit, which can leak resources. For more info, please see https://pytorch.org/docs/stable/distributed.html#shutdown (function operator())
===== ALL GPU TESTS PASSED =====
[16:34:25] <job> Succeeded
[16:34:27] verified: found success sentinel in pod log
[16:34:27] cleanup: deleting <job> (exit=0)
<job> "<job>" deleted

Verdict: PASSED

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

✅ GPU tests passed — vllm20-multi

2852 passed, 66 skipped, 24 warnings in 11971.23s (3:19:31)

Commit: d434cc524b890121b3c4a92d7edcd11184802ac0
Full run & artifact log

Last 40 log lines
tests/composer/test_compose_e2e.py:83
  /tmp/granite-switch/tests/composer/test_compose_e2e.py:83: PytestUnknownMarkWarning: Unknown pytest.mark.xdist_group - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.xdist_group("compose_e2e")

tests/composer/test_multi_audio_compose_e2e.py:124
  /tmp/granite-switch/tests/composer/test_multi_audio_compose_e2e.py:124: PytestUnknownMarkWarning: Unknown pytest.mark.xdist_group - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.xdist_group("multi_audio_compose_e2e")

tests/composer/test_multi_audio_compose_e2e.py:130
  /tmp/granite-switch/tests/composer/test_multi_audio_compose_e2e.py:130: PytestUnknownMarkWarning: Unknown pytest.mark.xdist_group - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.xdist_group("multi_audio_compose_e2e")

tests/composer/test_upstream_files.py:218
  /tmp/granite-switch/tests/composer/test_upstream_files.py:218: PytestUnknownMarkWarning: Unknown pytest.mark.xdist_group - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.xdist_group("upstream_build_e2e")

tests/integration/test_hf_to_vllm_weights.py::TestMultiSwitchForwardEquivalence::test_forward_logit_equivalence
  /tmp/granite-switch/.venv/lib/python3.12/site-packages/flashinfer/gemm/kernels/grouped_gemm_masked_blackwell.py:2059: DeprecationWarning: tcgen05.OperandMajorMode is deprecated, use cute.nvgpu.OperandMajorMode instead
    a_major_mode: tcgen05.OperandMajorMode,

tests/integration/test_hf_to_vllm_weights.py::TestMultiSwitchForwardEquivalence::test_forward_logit_equivalence
  /tmp/granite-switch/.venv/lib/python3.12/site-packages/flashinfer/gemm/kernels/grouped_gemm_masked_blackwell.py:2061: DeprecationWarning: tcgen05.OperandMajorMode is deprecated, use cute.nvgpu.OperandMajorMode instead
    b_major_mode: tcgen05.OperandMajorMode,

tests/integration/test_hf_to_vllm_weights.py::TestMultiSwitchForwardEquivalence::test_forward_logit_equivalence
tests/integration/test_hf_to_vllm_weights.py::TestMultiSwitchForwardEquivalence::test_forward_logit_equivalence
  /tmp/granite-switch/.venv/lib/python3.12/site-packages/flashinfer/gdn_kernels/blackwell/gated_delta_net_chunked.py:99: DeprecationWarning: tcgen05.OperandMajorMode is deprecated, use cute.nvgpu.OperandMajorMode instead
    from cutlass.cute.nvgpu.tcgen05 import OperandMajorMode

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
========= 2852 passed, 66 skipped, 24 warnings in 11971.23s (3:19:31) ==========
sys:1: DeprecationWarning: builtin type swigvarlink has no __module__ attribute
[rank0]:[W909 16:48:46.041874951 ProcessGroupNCCL.cpp:1575] Warning: WARNING: destroy_process_group() was not called before program exit, which can leak resources. For more info, please see https://pytorch.org/docs/stable/distributed.html#shutdown (function operator())
===== ALL GPU TESTS PASSED =====
[16:48:54] <job> Succeeded
[16:48:56] verified: found success sentinel in pod log
[16:48:56] cleanup: deleting <job> (exit=0)
<job> "<job>" deleted

Verdict: PASSED

@yairallouche
yairallouche merged commit 6013c7f into main Sep 10, 2026
6 checks passed
aviv1ron1 added a commit that referenced this pull request Sep 10, 2026
…ture

#131 removed SingleSwitch. test_granitemoe_audio_compose.py arrived from
feature/moe-audio-support, which branched before that, so it still parametrized
its fixture over ["single", "multi"] and asserted the switch class was
SingleSwitch on the single arm -- 9 errors after the merge, every one a [single]
case, while all 23 [multi] cases passed.

Now SWITCH_TYPES = ["multi"] and the assertion is unconditional, which is what
every other switch-parametrized file already does post-#131:
test_control_lut_refresh.py:34, tests/hf/test_multi_switch.py:49,
tests/vllm/test_multi_switch.py:52, tests/shared/gap_equivalence.py:15. So this
converges on the established shape rather than inventing one.

Nothing about the audio path changed -- the coverage that was single/multi
parametrized is engine-independent (the marker's embedding and output rows, the
control-LUT refresh, save/load survival), so dropping the removed engine loses no
assertion.

23 passed. The wider audio tier is 128 passed / 53 skipped with one error that is
this machine having no GPU ("Torch not compiled with CUDA enabled"), not a merge
regression.

Signed-off-by: aviv ron <rona@il.ibm.com>
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.

4 participants