Skip to content
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

construct only necessary elements in OffsetCalculator #55107

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 3 additions & 7 deletions aten/src/ATen/cuda/detail/OffsetCalculator.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,11 @@ struct OffsetCalculator {
// the strides will be in # of elements.
OffsetCalculator(int dims, const int64_t* sizes, const int64_t* const* strides, const int64_t* element_sizes=nullptr) : dims(dims) {
TORCH_CHECK(dims <= MAX_DIMS, "tensor has too many (>", MAX_DIMS, ") dims");
for (int i = 0; i < MAX_DIMS; ++i) {
if (i < dims) {
sizes_[i] = IntDivider<index_t>(sizes[i]);
} else {
sizes_[i] = IntDivider<index_t>(1);
}
for (int i=0; i < dims; i++){
sizes_[i] = IntDivider<index_t>(sizes[i]);
for (int arg = 0; arg < NARGS; arg++) {
int64_t element_size = (element_sizes == nullptr ? 1LL : element_sizes[arg]);
strides_[i][arg] = i < dims ? strides[arg][i] / element_size : 0;
strides_[i][arg] = strides[arg][i] / element_size;
}
Comment on lines 33 to 36
Copy link
Contributor

Choose a reason for hiding this comment

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

btw, might be worth emitting two loops here depending on whether element_sizes == nullptr to avoid a bunch of idiv instructions just to divide by 1.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point, I'll do that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Tried it, it actually increases instruction count, and in most cases element_sizes is not nullptr.
It is silly that TI build multiplies by element sizes and stores stride in bytes, and here we copy element_sizes into array (true story,

element_sizes[i] = iter.element_size(i);
) and then divide by them, but that's outside of this fix scope.

}
}
Expand Down