-
Notifications
You must be signed in to change notification settings - Fork 140
Using byte_arithmetic_ptr in interleaved_scan_kernel
#1437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
divyegala
wants to merge
15
commits into
rapidsai:main
Choose a base branch
from
divyegala:byte-array-interleaved
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f141e24
byte array in interleaved scan
divyegala a944636
Merge branch 'branch-25.12' into byte-array-interleaved
divyegala bfa1574
use functor and transform once
divyegala 8a42ab0
merge upstream
divyegala f278011
use implicit type casting
divyegala 3ba9c0a
delete double instantiations
divyegala 7fe48df
add prints
divyegala 35174e6
-s -v
divyegala 591f9d6
Merge branch 'branch-25.12' into byte-array-interleaved
divyegala ef65e5a
rename byte_array to byte_arithmetic_ptr
divyegala 60fba9a
make corrections
divyegala b09ecb1
post lambda as runtime param
divyegala 1247fa4
merge upstream
divyegala 9b0aceb
merge upstream
divyegala 571817b
delete one more
divyegala File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| 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; | ||
|
|
||
| __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 | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
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
databeing aligned - then we could use the lowest bit of it as the signedness tag and pass this struct everywhere in place of the pointers.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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:
139761184407553and it is not 2-byte aligned.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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::searchpublic API just passes these pointers along toivf_flat_interleaved_scanfunctionThere was a problem hiding this comment.
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
cuvs/cpp/src/neighbors/ivf_flat/ivf_flat_interleaved_scan.cuh
Line 926 in ef65e5a
There was a problem hiding this comment.
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 :)