Skip to content

dsv4/decode: consolidate per-family start_pos test sets + restore pattern default#697

Merged
zhangqi-chen merged 1 commit into
hw-native-sys:mainfrom
zhangqi-chen:dsv4-decode-start-set-consolidation
Jul 6, 2026
Merged

dsv4/decode: consolidate per-family start_pos test sets + restore pattern default#697
zhangqi-chen merged 1 commit into
hw-native-sys:mainfrom
zhangqi-chen:dsv4-decode-start-set-consolidation

Conversation

@zhangqi-chen

Copy link
Copy Markdown
Collaborator

Summary

  • Add three canonical per-attention-family start-position sets to decode_metadata.pyswa_decode_start_set, hca_decode_start_set, csa_decode_start_set — replacing the per-file init_default_start_pos fixtures.
  • Each set is order-preserving-deduped (S=1 collapses the -seq/-1 boundary pairs; some regimes coincide at the current constants) and front-loads the 8k long-context point so it survives batch < set-size truncation.
  • Flip --start-pos and build_tensor_specs defaults to None in the 7 standalone SWA/HCA/CSA attention, ratio-4/128 compressor, and indexer / indexer-compressor files, so the canonical per-batch set is used again. Previously the scalar DECODE_START_POS default silently bypassed the branch-covering pattern and only exercised the uniform 8k path. Explicit --start-pos N still overrides.
  • Trim redundant same-branch points: SWA drops two in-window interior samples; HCA drops the 3rd-block-crossing duplicate; CSA unchanged.
  • decode_layer and decode_fwd keep the uniform 8k default (integration tests).

Coverage is still capped at batch slots (decoupling from batch is left for a follow-up); sets are kept <= batch so nothing is silently dropped at B=8.

…tern default

Replace the per-file `init_default_start_pos` fixtures with three canonical
per-attention-family start-position sets in decode_metadata.py:
`swa_decode_start_set`, `hca_decode_start_set`, `csa_decode_start_set`.

- Each set is order-preserving-deduped (S=1 collapses the -seq/-1 boundary
  pairs; some regimes coincide at the current constants) and front-loads the
  8k long-context point so it survives batch<size truncation.
- Standalone SWA/HCA/CSA attention, ratio-4/128 compressors, and indexer /
  indexer-compressor now flip `--start-pos` and `build_tensor_specs` defaults
  to None so the canonical per-batch set is used again (previously the scalar
  DECODE_START_POS default silently bypassed the branch-covering pattern and
  only exercised the uniform 8k path). Explicit `--start-pos N` still overrides.
- Trim redundant same-branch points: SWA drops two in-window interior samples,
  HCA drops the 3rd-block-crossing duplicate; CSA unchanged.
- decode_layer and decode_fwd keep the uniform 8k default (integration tests).

Coverage is still capped at `batch` slots (decoupling from batch is left for a
follow-up); sets are kept <= batch so nothing is silently dropped at B=8.
@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: de6306f1-3c82-4f42-b3b8-22e6314a3ad7

📥 Commits

Reviewing files that changed from the base of the PR and between 628dcf6 and 0820084.

📒 Files selected for processing (8)
  • models/deepseek/v4/decode_attention_csa.py
  • models/deepseek/v4/decode_attention_hca.py
  • models/deepseek/v4/decode_attention_swa.py
  • models/deepseek/v4/decode_compressor_ratio128.py
  • models/deepseek/v4/decode_compressor_ratio4.py
  • models/deepseek/v4/decode_indexer.py
  • models/deepseek/v4/decode_indexer_compressor.py
  • models/deepseek/v4/decode_metadata.py

📝 Walkthrough

Walkthrough

Seven DeepSeek-V4 decode test harnesses (attention CSA/HCA/SWA, compressor ratio4/ratio128, indexer, indexer compressor) are updated to remove the DECODE_START_POS import and default build_tensor_specs's start_pos parameter to None. A new module decode_metadata.py adds _tile_starts and three factory functions (swa_decode_start_set, hca_decode_start_set, csa_decode_start_set) that generate canonical per-batch start-position tensors, replacing hardcoded pattern tensors. CLI --start-pos defaults and help text are updated accordingly across all harnesses.

Changes

Canonical Decode Start-Position Sets

Layer / File(s) Summary
Start-position factory functions
models/deepseek/v4/decode_metadata.py
Adds _tile_starts helper and three factories (swa_decode_start_set, hca_decode_start_set, csa_decode_start_set) that build attention-family-specific position patterns and return tiled int32 tensors; expands config imports accordingly.
CSA attention harness wiring
models/deepseek/v4/decode_attention_csa.py
build_tensor_specs defaults start_pos to None; init_default_start_pos and CLI default/help text now use csa_decode_start_set.
HCA attention harness wiring
models/deepseek/v4/decode_attention_hca.py
Same pattern applied using hca_decode_start_set.
SWA attention harness wiring
models/deepseek/v4/decode_attention_swa.py
Same pattern applied using swa_decode_start_set.
Ratio-128 compressor harness wiring
models/deepseek/v4/decode_compressor_ratio128.py
Same pattern applied using hca_decode_start_set.
Ratio-4 compressor harness wiring
models/deepseek/v4/decode_compressor_ratio4.py
Same pattern applied using csa_decode_start_set.
Indexer harness wiring
models/deepseek/v4/decode_indexer.py
Same pattern applied using csa_decode_start_set.
Indexer compressor harness wiring
models/deepseek/v4/decode_indexer_compressor.py
Same pattern applied using csa_decode_start_set.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

Poem

A rabbit hops through decode's maze,
Ditching fixed points for canonical ways,
Eight-k anchors, windows wide,
csa, hca, swa now guide,
Tidier starts for testing days! 🐇✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the main change: consolidating per-family start-position sets and restoring canonical defaults in decode tests.
Description check ✅ Passed The description accurately summarizes the new canonical start-position sets and default behavior changes across the affected files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request refactors the start-position logic across various DeepSeek v4 decode modules (CSA, HCA, SWA, compressors, and indexers) by introducing canonical start-position helper functions in decode_metadata.py. This replaces duplicate hardcoded patterns and updates CLI argument defaults. Feedback on the changes suggests vectorizing the _tile_starts helper function in decode_metadata.py using PyTorch operations instead of a Python loop to improve efficiency and idiomatic code quality.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +57 to +65
def _tile_starts(pattern: list[int], batch: int) -> torch.Tensor:
uniq: list[int] = []
for p in pattern:
if p not in uniq:
uniq.append(int(p))
vals = torch.empty((batch,), dtype=torch.int32)
for b in range(batch):
vals[b] = uniq[b % len(uniq)]
return vals

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.

medium

Using a Python loop to assign individual elements of a PyTorch tensor is inefficient and unidiomatic. We can vectorize this operation by creating a tensor from the unique elements, repeating it to cover the batch size, and slicing it to the exact batch size.

def _tile_starts(pattern: list[int], batch: int) -> torch.Tensor:
    uniq: list[int] = []
    for p in pattern:
        if p not in uniq:
            uniq.append(int(p))
    uniq_tensor = torch.tensor(uniq, dtype=torch.int32)
    return uniq_tensor.repeat((batch + len(uniq) - 1) // len(uniq))[:batch].clone()

@zhangqi-chen zhangqi-chen merged commit 16d0137 into hw-native-sys:main Jul 6, 2026
8 of 13 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.

1 participant