Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/test_python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pytest \
--cov=cuvs \
--cov-report=xml:"${RAPIDS_COVERAGE_DIR}/cuvs-coverage.xml" \
--cov-report=term \
-s -v \
tests

rapids-logger "pytest cuvs-bench"
Expand Down
2 changes: 1 addition & 1 deletion ci/test_wheel_cuvs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ rapids-pip-retry install \
"${LIBCUVS_WHEELHOUSE}"/libcuvs*.whl \
"$(echo "${CUVS_WHEELHOUSE}"/cuvs*.whl)[test]"

python -m pytest ./python/cuvs/cuvs/tests
python -m pytest ./python/cuvs/cuvs/tests -s -v
6 changes: 2 additions & 4 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -462,12 +462,10 @@ if(NOT BUILD_CPU_ONLY)
src/neighbors/ivf_flat/ivf_flat_search_uint8_t_int64_t.cu
src/neighbors/ivf_flat/ivf_flat_interleaved_scan_float_int64_t.cu
src/neighbors/ivf_flat/ivf_flat_interleaved_scan_half_int64_t.cu
src/neighbors/ivf_flat/ivf_flat_interleaved_scan_int8_t_int64_t.cu
src/neighbors/ivf_flat/ivf_flat_interleaved_scan_uint8_t_int64_t.cu
src/neighbors/ivf_flat/ivf_flat_interleaved_scan_int8_uint8_t_int64_t.cu
src/neighbors/ivf_flat/ivf_flat_interleaved_scan_float_int64_t_bitset.cu
src/neighbors/ivf_flat/ivf_flat_interleaved_scan_half_int64_t_bitset.cu
src/neighbors/ivf_flat/ivf_flat_interleaved_scan_int8_t_int64_t_bitset.cu
src/neighbors/ivf_flat/ivf_flat_interleaved_scan_uint8_t_int64_t_bitset.cu
src/neighbors/ivf_flat/ivf_flat_interleaved_scan_int8_uint8_t_int64_t_bitset.cu
src/neighbors/ivf_flat/ivf_flat_serialize_float_int64_t.cu
src/neighbors/ivf_flat/ivf_flat_serialize_half_int64_t.cu
src/neighbors/ivf_flat/ivf_flat_serialize_int8_t_int64_t.cu
Expand Down
129 changes: 129 additions & 0 deletions cpp/include/cuvs/core/byte_arithmetic_ptr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright (c) 2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <cstdint>

namespace cuvs::detail {

struct byte_arithmetic_ptr {
void* data = nullptr;
bool is_signed = false;
Comment on lines +24 to +25
Copy link
Contributor

Choose a reason for hiding this comment

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

Passing this on gpu will probably require three words, right? This could have the register usage consequence in some edge cases. If only could we rely on the data being aligned - then we could use the lowest bit of it as the signedness tag and pass this struct everywhere in place of the pointers.

Copy link
Member Author

@divyegala divyegala Oct 20, 2025

Choose a reason for hiding this comment

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

The data being aligned doesn't guarantee that the lowest bit will be set in the int8_t case, right? If the first int8 value is positive then the sign bit won't be set.

Unless I misunderstood what you are saying?

We could always try to pass this as a reference to device functions instead, if that's helpful.

Copy link
Contributor

Choose a reason for hiding this comment

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

The pointers will almost surely be aligned, but technically one can pass an unaligned pointer since the data is one byte granularity. My question is would it be acceptable for us to always assume it's at least two-byte aligned (or make an alignment check somewhere and throw an error otherwise)?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it should be acceptable for us to check that. In the case of alignment, how do you propose we check for signed-ness?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd do something along the lines of this:

struct byte_arithmetic_ptr {
 private:
    constexpr static uintptr_t kSignMask = 0x1;
    uintptr_t value_;
  public:
    byte_arithmetic_ptr(uint8_t* ptr): value_(reinterpret_cast<uintptr_t>(ptr) {}
    byte_arithmetic_ptr(int8_t* ptr): value_(reinterpret_cast<uintptr_t>(ptr | kSignMask) {}
   
    constexpr void* get_data() {
      return reinterpret_cast<void*>(value_ & ~kSignMask);
    }
    constexpr bool is_signed() const {
      return (value_ & kSignMask) == kSignMask;
    }
};

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately, it looks like even our gtests don't pass. Here's an int8 address: 139761184407553 and it is not 2-byte aligned.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have these pointers passed down from the public api anywhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, what do you mean? The ivf_flat::search public API just passes these pointers along to ivf_flat_interleaved_scan function

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, so that's the query pointer what causes the problem (the ivf lists are aligned). Then it's debatable whether it makes sense to require it to be aligned. Could be good for performance, but (1) loading queries isn't a bottleneck, (2) it would require changes to the code where we increment by potentially odd offset

query = query + query_id * dim;

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we can keep this in mind for later, in case performance becomes an issue? I just posted some performance #s in the description body and IMO they look pretty good :)


__host__ __device__ byte_arithmetic_ptr(void* ptr, bool signed_flag)
: data(ptr), is_signed(signed_flag)
{
}

// Proxy that references an element in the array
struct byte {
byte_arithmetic_ptr* parent = nullptr;
int64_t idx = -1;
uint8_t value = 0; // used for detached proxies

// Constructor for live proxy
__host__ __device__ byte(byte_arithmetic_ptr& p, int64_t i) : parent(&p), idx(i) {}

// Copy constructor: detached copy stores the current value
__host__ __device__ byte(const byte& other)
: parent(nullptr), idx(-1), value(static_cast<uint8_t>(other))
{
}

// Copy assignment: detached copy stores value
__host__ __device__ byte& operator=(const byte& other)
{
parent = nullptr;
idx = -1;
value = static_cast<uint8_t>(other);
return *this;
}

// Deleted move operations
__host__ __device__ byte(byte&& other) = delete;
__host__ __device__ byte& operator=(byte&& other) = delete;

// Conversion to uint8_t
__host__ __device__ operator uint8_t() const
{
if (parent) {
if (parent->is_signed) {
int8_t val = reinterpret_cast<int8_t*>(parent->data)[idx];
return static_cast<uint8_t>(static_cast<int16_t>(val) + 128);
} else {
return reinterpret_cast<uint8_t*>(parent->data)[idx];
}
} else {
return value; // return local value if detached
}
}

// Assignment from uint8_t
__host__ __device__ byte& operator=(uint8_t normalized_value)
{
if (parent) {
if (parent->is_signed) {
reinterpret_cast<int8_t*>(parent->data)[idx] =
static_cast<int8_t>(static_cast<int16_t>(normalized_value) - 128);
} else {
reinterpret_cast<uint8_t*>(parent->data)[idx] = normalized_value;
}
} else {
value = normalized_value; // store in local value if detached
}
return *this;
}
};

// Non-const index access: returns live proxy
__host__ __device__ byte operator[](int64_t idx) { return byte(*this, idx); }

// Const index access: returns immediate value
__host__ __device__ uint8_t operator[](int64_t idx) const
{
if (is_signed) {
int8_t val = reinterpret_cast<int8_t*>(data)[idx];
return static_cast<uint8_t>(static_cast<int16_t>(val) + 128);
} else {
return reinterpret_cast<uint8_t*>(data)[idx];
}
}

// Dereference (like *ptr)
__host__ __device__ uint8_t operator*() const { return (*this)[0]; }
__host__ __device__ byte operator*() { return byte(*this, 0); }

// Pointer arithmetic
__host__ __device__ byte_arithmetic_ptr operator+(int64_t offset) const
{
if (is_signed)
return byte_arithmetic_ptr(static_cast<int8_t*>(data) + offset, true);
else
return byte_arithmetic_ptr(static_cast<uint8_t*>(data) + offset, false);
}

__host__ __device__ bool operator==(const byte_arithmetic_ptr& other) const
{
return data == other.data;
}
__host__ __device__ bool operator!=(const byte_arithmetic_ptr& other) const
{
return !(*this == other);
}
};

} // namespace cuvs::detail
Loading
Loading