Repository navigation
Support 2^N * 9 dimensions in randomHadamardTransform without padding - #110910
Conversation
randomHadamardTransform already avoids zero-padding for lengths 2^k * m with
m in {12, 20} by applying the exact Kronecker product H_(2^k) (x) H_m, where
H_m is a Paley +-1 Hadamard matrix. That does not cover the common 2^N * 9
family (e.g. 1152 = 128 * 9, used by some embedding models), because order 9
has no +-1 Hadamard matrix -- those exist only for orders 1, 2, and multiples
of 4 -- so such inputs were padded (1152 -> 2048).
Add an exact, non-padding path for length = 2^k * 9. The order-9 small factor
is a real orthogonal Discrete Hartley Transform matrix C_9 with entries
cas(2*pi*i*j/9) = cos(...) + sin(...). The DHT matrix is real, symmetric, and
self-inverse up to scale (C^T C = 9 * I), so it is orthogonal up to the
1/sqrt(m) factor -- exactly the unnormalized +-1 Hadamard convention already
used here, so the final 1/sqrt(k) scaling makes the whole transform
norm-preserving with no extra bookkeeping. It is applied as the exact Kronecker
product H_(2^k) (x) C_9: a dense float mat-vec per 9-element block followed by
the cross-block fast Walsh-Hadamard transform. This keeps the input dimension
instead of growing to the next power of two.
The C_9 block uses float multiplies (a +-1 sign-flip kernel is impossible at
order 9) and a scalar cross-block FWHT (the NEON block kernel needs the lane
count to be a multiple of 4, which 9 is not), so the DHT path is
kernel-independent -- verified bit-for-bit between the scalar and NEON kernels.
The exact per-coordinate output was cross-checked against an independent NumPy
model (splitmix64 signs, DHT-9, cross-block FWHT, 1/sqrt(k) scale).
Add test 04512_random_hadamard_transform_hartley_9 and update the docs.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
# Conflicts: # src/Functions/randomHadamardTransform.cpp
|
Workflow [PR], commit [f1be4f1] Summary: ✅
AI ReviewSummaryThis PR adds an exact order-9 Hartley path for PR Metadata
Findings❌ Blockers
Final VerdictStatus: Minimum required actions:
|
|
The two red stress checks are both unrelated to this PR (which only touches
@groeneai, could you investigate the |
|
Investigated. Tracking issue: #110927. CIDB confirms your read: this signature has exactly one occurrence in 60 days, only on this branch's Root cause: in Fix direction: treat the null step in the bucketed branch as a valid empty read (return an empty step carrying the serialized header, like the existing |
|
Fix PR: #110929 (Closes #110927). One refinement from the initial direction above: returning a |
… a regression test The AI review (clickhouse-gh bot, "Request changes") asked for a focused regression proving the `Quantized(..., 1152)` codec keeps its old projection now that the shared `HadamardTransform::kroneckerFactorFor` helper knows the order-9 Hartley factor. `VectorQuantizer.cpp`'s `hadamardFactorFor` wrapper deliberately filters that factor down to `SmallFactorKind::Hadamard`, so a 1152 = 128 * 9 vector stays on the "zero-pad to 2048" projection instead of the exact order-9 Hartley path used by `randomHadamardTransform`. Switching it would silently rewrite every already-stored `Quantized(..., 1152)` code. The existing codec coverage (`02354_vector_search_quantize_codec_kronecker`, a 96 = 8 * 12 exact-Hadamard case) cannot detect this: a plain encode/query round-trip agrees whether both sides pad or both take Hartley. The new test `02354_vector_search_quantize_codec_hartley_guard` instead pins an invariant that distinguishes the two paths. For the padded path, encoding a 1152-dim vector produces exactly the same structured projection (same seed, same working dimension 2048, same FWHT) as encoding that vector zero-padded to 2048 dims, so the 1152 / 8 = 144 packed `rabitq` sign bytes must be byte-identical to the leading 144 sign bytes of the 2048-dim code. The exact Hartley path would produce different signs and break this equality. The comparison is between two codes produced by the same binary, so it is robust to per-platform floating-point differences (no absolute byte values pinned). Verified end to end: the test passes on the current code; patching `hadamardFactorFor` to return `kroneckerFactorFor` directly (the exact refactor the guard prevents) makes the test fail, and the codec's Hadamard-only kernel then throws `INCORRECT_DATA` on the order-9 factor. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The `signs_match_padded_2048` check read `vec.quantized` through a `(SELECT id, vec FROM ...)` subquery. The `.quantized` subcolumn does not survive that subquery boundary under the old analyzer, so the query failed with `Unknown column: vec.quantized` (`UNKNOWN_IDENTIFIER`), exactly as the `code_length_1152` query on the line above (which reads the subcolumn directly from the table) does not. Read `vec.quantized` directly from each table and compare the per-id codes with an `ALL INNER JOIN` plus `min`, which also removes the dependence on `groupArray` preserving the subquery ordering. The invariant is unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The exact order-9 Hartley small factor `C_9` is orthogonal, so the full
transform stays norm-preserving, but its rows do not have the uniform leverage
of a `+-1` Hadamard matrix. A truncated prefix of `C_9` rows is therefore a
position-biased projection instead of a valid Johnson-Lindenstrauss / SRHT one:
for `length = 18`, `output_dims = 2` a one-hot input on lane `1` keeps squared
norm `~1.49` while lane `8` keeps only `~0.51`, and the diagonal sign only flips
signs so it cannot average that out. The `1/sqrt(output_dims)` scaling that the
function documents as norm-preserving-in-expectation only holds for the flat
`+-1` factors.
So the exact `C_9` path is now kept only for the full transform; a genuine
truncation (`output_dims < length`) of the `2^k * 9` family falls back to the
zero-padded power-of-two FWHT, whose `+-1` rows do have uniform leverage. The
`+-1` Hadamard Kronecker factors (`m` in {12, 20}) are unaffected, and an
`output_dims` above the input length still throws `ARGUMENT_OUT_OF_BOUND` in
every case (consistent with the exact `2^k * 12` and `2^k * 20` paths).
The docs (embedded and `array-functions.mdx`) are updated to match, and a new
test `04611_random_hadamard_transform_hartley_truncation` pins the invariant:
every one-hot basis vector of a length-18 input, projected to `output_dims = 2`,
has the same squared norm.
Addresses the review at `randomHadamardTransform.cpp:219`.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 95/95 (100.00%) · Uncovered code |
|
📊 Cloud Performance Report ✅ AI verdict: This PR only touches randomHadamardTransform and the vector-search quantization codec (plus docs and tests); none of the flagged ClickBench queries (Q4, Q15, Q30, Q31) execute that code, so neither the ~-20% swings on Q4/Q15 nor the ~+16-17% swings on Q30/Q31 can be a real effect of this change. All four deltas sit within master's current variance band and were correctly suppressed as no_change / not_sure. No performance action is needed for this PR. clickbenchFlagged queries (2 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
|
This function was added in 26.7, so no problem with compatibility. |
Support 2^N * 9 dimensions in randomHadamardTransform without padding
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
randomHadamardTransformnow transforms vectors whose length is2^N * 9(for example1152 = 128 * 9, common in some embedding models) exactly, without zero-padding to the next power of two, so the output keeps the input dimension.randomHadamardTransformalready avoids zero-padding for lengths2^k * mwithmin{12, 20}by applying the exact Kronecker productH_(2^k) (x) H_m, whereH_mis a Paley+/-1Hadamard matrix. That does not cover the common2^N * 9family (e.g.1152 = 128 * 9), because order 9 has no+/-1Hadamard matrix — those exist only for orders 1, 2, and multiples of 4 — so such inputs were padded (1152 -> 2048).This PR adds an exact, non-padding path for
length = 2^k * 9. The order-9 small factor is a real orthogonal Discrete Hartley Transform matrixC_9with entriescas(2*pi*i*j/9) = cos(...) + sin(...). The DHT matrix is real, symmetric, and self-inverse up to scale (C^T C = 9 * I), so it is orthogonal up to the1/sqrt(m)factor — exactly the unnormalized+/-1Hadamard convention already used here, so the existing final1/sqrt(k)scaling makes the whole transform norm-preserving with no extra bookkeeping. It is applied as the exact Kronecker productH_(2^k) (x) C_9: a dense float mat-vec per 9-element block followed by the cross-block fast Walsh-Hadamard transform.Notes:
C_9block uses float multiplies (a+/-1sign-flip kernel is impossible at order 9) and a scalar cross-block FWHT (the NEON block kernel needs the lane count to be a multiple of 4, which 9 is not), so the DHT path is kernel-independent — verified bit-for-bit between the scalar and NEON kernels.1/sqrt(k)scale).New test:
04512_random_hadamard_transform_hartley_9.Version info
26.7.1.1193(included in26.7and later)