Skip to content

feat(codec): PR-X12 v1 — λ-RDO mode selection + rANS entropy coder#211

Merged
AdaWorldAPI merged 3 commits into
masterfrom
claude/continue-ndarray-x0Oaw
May 27, 2026
Merged

feat(codec): PR-X12 v1 — λ-RDO mode selection + rANS entropy coder#211
AdaWorldAPI merged 3 commits into
masterfrom
claude/continue-ndarray-x0Oaw

Conversation

@AdaWorldAPI
Copy link
Copy Markdown
Owner

@AdaWorldAPI AdaWorldAPI commented May 27, 2026

Summary

Completes the x265-shaped cognitive codec (ndarray::hpc::codec) to a usable v1 by adding the two stages the board tracked as the outstanding PR-X12 debt — RDO and ANS:

  • codec::rdo (A6) — λ-rate-distortion mode selection. Scores all four modes by cost = (rate_bytes << 8) + λ_q8 · distortion and picks the minimum. It is the soft, cost-weighted generalization of predict_intra's hard decision tree. Uses an integer fixed-point λ (lambda_q8 = λ × 256) instead of the design doc's f32, giving deterministic, cross-platform-bit-exact mode decisions consistent with the substrate's no-float discipline.
  • codec::ans (A7) — static per-block rANS entropy coder over the 4-symbol mode alphabet (Skip/Merge/Delta/Escape). Self-contained stream layout (symbol count + normalized 4×u16 freq table + rANS payload); bit-exact round-trip. Chosen over CABAC per the design's open-Q1 ruling (cache-friendlier, no per-bit context branches).

Both stages are grounded against the existing integer-only LeafCu/CellMode/MergeDir and the 2/3/3/6-byte wire format (mode.rs). No unsafe, no float in either module.

Scope boundary (per the design doc): transform (A4) stays deferred to v2 — open-Q2 ruled a 1-D DCT doesn't help an 8-bit scalar residual. stream (A8, byte-stream framing over ans) is the remaining follow-on.

Test plan

  • cargo test -p ndarray --features std,codec --lib hpc::codec — 81 lib tests pass (incl. rANS round-trip on empty/single/all-same/mixed/large-random/skewed streams; RDO λ-sweep, merge-closest, escape-cursor, cost-formula).
  • cargo test -p ndarray --features std,codec --doc hpc::codec — 20 doctests pass.
  • cargo clippy -p ndarray --features std,codec --lib -- -D warnings — clean.
  • cargo fmt — clean.

https://claude.ai/code/session_01HbqooFZHAjaUtFEzhA1R2u


Generated by Claude Code

Summary by CodeRabbit

  • New Features

    • Added rANS-based entropy codec for compact, validated mode-tag streams, with robust handling of truncated or malformed inputs.
    • Added rate–distortion selector to deterministically choose per-cell codec modes using fixed-point cost scoring.
  • Documentation

    • Updated codec module docs to describe the new RDO and rANS components and current status of remaining work.
  • Tests

    • Added comprehensive unit tests covering round-trip, edge cases, and rejection scenarios.

Review Change Stack

Completes the x265-shaped cognitive codec to a usable v1 by adding the
two stages the board tracked as missing (RDO + ANS):

- `codec::rdo` (A6) — λ-rate-distortion mode selection: scores all four
  modes by `(rate << 8) + λ_q8·distortion` and picks the minimum. The
  soft, cost-weighted generalization of `predict_intra`'s hard tree. Uses
  an integer fixed-point λ (λ × 256) rather than the design's f32, for
  deterministic, cross-platform-bit-exact decisions consistent with the
  substrate's no-float discipline.
- `codec::ans` (A7) — static-table rANS over the 4-symbol mode alphabet
  (Skip/Merge/Delta/Escape). Self-contained stream (count + normalized
  freq table + payload); bit-exact round-trip. Chosen over CABAC per the
  design's open-Q1 ruling.

Grounded against the existing integer-only LeafCu/CellMode/MergeDir and
the 2/3/3/6-byte wire format; transform (A4) stays deferred to v2 per
design Q2, stream (A8) is the remaining follow-on.

81 lib tests + 20 doctests green; clippy -D warnings clean.

https://claude.ai/code/session_01HbqooFZHAjaUtFEzhA1R2u
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 27, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: ec810746-e6db-4a5b-bd95-275af300ebd7

📥 Commits

Reviewing files that changed from the base of the PR and between 347f36d and ed6f3ba.

📒 Files selected for processing (1)
  • src/hpc/codec/ans.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/hpc/codec/ans.rs

📝 Walkthrough

Walkthrough

Adds a fixed-point RDO selector for per-cell mode choices and a static-table rANS encoder/decoder for the 4-symbol mode stream, plus module wiring and a blackboard status update.

Changes

RDO and rANS Codec Implementation

Layer / File(s) Summary
RDO mode selection and scoring
src/hpc/codec/rdo.rs
Defines RdoConfig (fixed-point λ), RdoContext (per-cell inputs), and RdoChoice. Implements rdo_select to score Skip/Merge/Delta/Escape with cost cost_q8 = (rate_bytes << 8) + lambda_q8 * distortion, enforces Merge neighbour rules and Escape cursor post-increment only when Escape strictly wins, and includes comprehensive unit tests.
rANS entropy codec implementation
src/hpc/codec/ans.rs
Adds RansFreqTable with normalized frequency construction and cumulative sums, rans_encode/rans_decode core routines, and stream APIs encode_modes/decode_modes using a header (4-byte count + 4×u16 freqs) plus payload. Defensive decoding handles truncated payload bytes. Test suite verifies invariants, round-trips, and malformed inputs.
Codec module integration and wiring
src/hpc/codec/mod.rs, .claude/blackboard.md
Module documentation expanded to include RDO and rANS; new public submodules ans and rdo declared and re-exported symbols added. Blackboard status updated to mark RDO (A6) and rANS (A7) as complete and to defer transform/stream.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 I nibble bits and tune the tune,

Fixed-point hops beneath the moon,
RDO chooses where to stay,
rANS tucks modes safe away,
Tiny bytes, a tidy rune.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main changes: it specifies the addition of λ-RDO mode selection and rANS entropy coder, which are the two primary features completing PR-X12 v1 as described in the objectives.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/continue-ndarray-x0Oaw

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

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 52e44f5af7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/hpc/codec/ans.rs
let hi = stream[5 + 2 * i];
*slot = u16::from_le_bytes([lo, hi]);
}
let table = RansFreqTable::from_freqs(freq)?;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reject empty frequency tables for nonempty streams

When the header says n > 0 but all four stored frequencies are zero, from_freqs still succeeds here and rans_decode then fabricates Escape symbols from a zero-frequency table instead of treating the stream as malformed. This lets a corrupt/non-encode_modes stream with a nonempty count and an empty model decode to bogus mode tags; add a n == 0/sum consistency check before decoding.

Useful? React with 👍 / 👎.

Codex P2: decode_modes accepted a stream whose header claimed n > 0 while
all four stored frequencies were zero (an "empty model"). from_freqs
permits an all-zero table for the n == 0 empty-stream case, so a corrupt
or non-encode_modes stream slipped through and rans_decode fabricated
Escape tags from the freq-0 table instead of reporting malformed input.
Add an explicit n > 0 / all-zero-table guard + regression test.

https://claude.ai/code/session_01HbqooFZHAjaUtFEzhA1R2u
@AdaWorldAPI AdaWorldAPI merged commit 0129b5c into master May 27, 2026
18 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.

2 participants