Skip to content

Commit 0315d9b

Browse files
Add check for negative index in cpu kernel op_index
Fast path cannot be taken when the index is negative in op_index. It results in a failure in `check_fast_path_args`. Add a check to `check_fast_path_conditions` for negative index and return `false` if one is encountered. Change-Id: I6c3fb1793da9a0831ef41a47dc2ab4423aa38be3 Signed-off-by: Sebastian Larsson <[email protected]>
1 parent fdfeaa4 commit 0315d9b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

kernels/portable/cpu/op_index.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright (c) Meta Platforms, Inc. and affiliates.
33
* All rights reserved.
4+
* Copyright 2025 Arm Limited and/or its affiliates.
45
*
56
* This source code is licensed under the BSD-style license found in the
67
* LICENSE file in the root directory of this source tree.
@@ -47,6 +48,23 @@ bool check_fast_path_conditions(
4748
if (index.dim() != 1) {
4849
return false;
4950
}
51+
52+
// Fast path only supports non-negative indices.
53+
if (ix_type == ScalarType::Int) {
54+
const int32_t* const data = index.const_data_ptr<int32_t>();
55+
for (const auto j : c10::irange(index.numel())) {
56+
if (data[j] < 0) {
57+
return false;
58+
}
59+
}
60+
} else { // ScalarType::Long
61+
const int64_t* const data = index.const_data_ptr<int64_t>();
62+
for (const auto j : c10::irange(index.numel())) {
63+
if (data[j] < 0) {
64+
return false;
65+
}
66+
}
67+
}
5068
}
5169
}
5270

0 commit comments

Comments
 (0)