[Refactor] KernelElem trait to deduplicate f32/f64 stubs

Open
#1,433 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

@andrewmusselman is already working on this.

Since Oct 20, 2026.

  • #1555 by @andrewmusselman — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
50/100
Issue type
Refactor
Clarity
Clearly specified
Activity status
Quiet
Tech stack
rust

Research direction

Start by reading qdp-kernels/src/lib.rs and the proposed qdp-kernels/src/elem.rs design, then compare the paired FFI stubs and the f64-only symbols. Inspect qdp-core/src/gpu/encodings/angle.rs around encode_batch_async_pipeline and review issue #1310 for trait-surface overlap. Run cargo test -p qdp-kernels and the qdp-core GPU tests; done means reduced duplication, preserved FFI names and CUDA behavior, and no more than 2% amplitude f32 throughput regression.

Written by the indexing model from the issue text.

Description

bug enhancement
What

Collapse the copy-paste *_f32 FFI stubs in qdp-kernels/src/lib.rs behind a KernelElem trait plus a define_stub! macro, and merge the two near-identical encode_batch_async_pipeline functions in angle.rs into one generic function.

Why

qdp-kernels/src/lib.rs is 726 lines, much of it mechanical duplication: the encode/norm FFI symbols exist as f64/f32 pairs (launch_amplitude_encode + _f32, and likewise for angle, basis, l2_norm) differing only in element type. qdp-core/src/gpu/encodings/angle.rs repeats the pattern: encode_batch_async_pipeline (lines 742–803) and encode_batch_async_pipeline_f32 (lines 806–867) are two ~61-line near-clones. This duplication is pure maintenance tax — every change has to be made twice and the copies drift. A KernelElem trait + macro expresses the f32/f64 pair once.

How
  • Add qdp-kernels/src/elem.rs defining a KernelElem trait with impl KernelElem for f32 and impl KernelElem for f64, capturing the per-type bits (element type, suffix, FFI symbol mapping).
  • Replace the paired stub block with a define_stub! macro invoked once per FFI symbol, generating both the f64 and f32 entry points from a single definition.
    • Note: not every symbol is paired — launch_iqp_encode/launch_iqp_encode_batch and launch_phase_encode/launch_phase_encode_batch are f64-only. The macro must support declaring an f64-only symbol (no forced _f32 twin), so it can't assume every symbol has both.
  • Collapse encode_batch_async_pipeline and encode_batch_async_pipeline_f32 in angle.rs into one generic fn encode_batch_async_pipeline<T: KernelElem>(...).
  • Coordinate with #1310 (single-sample f32 on QuantumEncoder) so the trait surface does not overlap or conflict.
  • Keep the basis launcher's index parameter as usize (not T): the KernelElem type parameter governs state/feature dtype only, never the basis index, so index exactness is preserved regardless of f32/f64. (See the Track A basis-precision amendment.)
  • Keep CUDA kernel algorithms untouched — this is a Rust-side dedup only.

Out of scope: Changing CUDA kernel algorithms, performance tuning beyond keeping parity.

Acceptance criteria:

  • cargo test -p qdp-kernels and the qdp-core GPU tests pass
  • Net lines-of-code reduction in lib.rs and angle.rs
  • Throughput regression ≤2% on the amplitude f32 baseline
  • No change to CUDA kernel behavior or to public FFI symbol names (paired and f64-only symbols alike)
Dominant language
Rust
Stars
2.3k
Forks
1k
Avg merge
2d 20h
Merged PRs (30d)
7

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from apache/mahout

All issues in apache/mahout

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.