Repository navigation
vector_of_kll: convert array inputs to the expected dtype - #73
Merged
Merged
Conversation
The query methods took array arguments as an untyped nb::ndarray<> and input_to_vec() wrapped the buffer as the expected element type without checking its dtype. A float32 ranks array was read as float64, a float64 values array was read as float32 by the floats sketches, and an int64 index array (numpy's default int) was read as int32, all silently. Type the array alternative of ArrInputType as nb::ndarray<V, nb::ndim<1>> so nanobind converts the input to the expected dtype. This fixes get_quantiles, get_ranks, get_pmf, get_cdf, and the isk index argument of every method that takes one. Fixes #63. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tisonkun
approved these changes
Oct 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #63.
Problem
The
vector_of_kll_*_sketchesquery methods accept a scalar, a list, or a numpy array. The array alternative was an untypednb::ndarray<>, andinput_to_vec()wrapped its buffer as the expected element type without checking the dtype, so a mismatched array was silently reinterpreted. Reproduced on main:vector_of_kll_floats_sketchesget_quantiles(np.float32 ranks)(the issue)[-98.9, -99.5][-99.2, -10.0], same as float64get_ranks(np.float64 values)[1.0, 1.0]isk=np.array([0, 1])(int64, numpy's default)update()was not affected: it already takes a typednb::ndarray<T>, which nanobind converts.Fix
ArrInputTypenow usesnb::ndarray<V, nb::ndim<1>>for its array alternative, so nanobind converts the input to the expected dtype before the method sees it. This coversget_quantiles,get_ranks,get_pmf,get_cdf, and theiskargument of every method that takes it (collapse,serialize, ...).One behavior change: a 2-D array passed where a 1-D input is expected now raises
TypeError. Before, its buffer was reinterpreted as 1-D.Testing
New
test_kll_input_dtypeschecks that float32 and float64 arrays give the same results as lists forget_quantiles,get_ranks,get_pmfandget_cdf, and that int32 and int64 index arrays select the same sketches as a list. It fails on main and passes with this change; all 39 tests pass (macOS, Python 3.12). The ranks in the test are exactly representable in float32 (0.0625,0.9375); a value likenp.float32(0.99)is genuinely a different rank from0.99and can select a different item.🤖 Generated with Claude Code