Skip to content

Commit 2ac5836

Browse files
karan1508facebook-github-bot
authored andcommitted
Fix sign-compare errors in quantized cpu kernels for Zephyr/GCC builds
Summary: ## Why? The Zephyr firmware target `tycho_t3c-tsn_graph_simulator_mps3_an547_cmake` failed to compile `xplat/executorch/kernels/quantized/cpu/embeddingxb.cpp` under `-Werror=sign-compare`. The loop variable was declared `size_t` (unsigned) but compared against `Tensor::dim()`, which returns `ssize_t` (signed). On 64-bit host toolchains the warning is often suppressed or both types match in width, so the regression slipped in via D90402567 (int32 indices support). On the 32-bit Zephyr ARM target the mismatch is fatal. Several sibling kernels in the same directory have the identical latent pattern and would break the moment they get pulled into a Zephyr build path. ## What? Match the loop variable's type to the signed return type of `Tensor::dim()` by switching `size_t` to `ssize_t` everywhere this pattern appears in the quantized cpu kernels and two test files. This is also the safer pattern — casting to `size_t` would turn a negative `dim()` into `SIZE_MAX` and overflow the fixed-size stack buffer used for sizes. The change is mirrored across the xplat and fbcode copies so the diff_train sync stays consistent. Differential Revision: D105701588
1 parent 1d754c8 commit 2ac5836

6 files changed

Lines changed: 6 additions & 6 deletions

File tree

kernels/quantized/cpu/embeddingxb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ void resize_out_tensor(
224224
Tensor& out,
225225
int weight_nbit) {
226226
executorch::aten::SizesType expected_output_size[kTensorDimensionLimit];
227-
for (size_t i = 0; i < indices.dim(); i++) {
227+
for (ssize_t i = 0; i < indices.dim(); i++) {
228228
expected_output_size[i] = indices.size(i);
229229
}
230230
const size_t embedding_dim = get_embedding_dim(weight.size(1), weight_nbit);

kernels/quantized/cpu/op_dequantize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ Tensor& dequantize_per_token_out(
590590
Tensor& out) {
591591
// Refactor this into a util
592592
size_t num_channels = 1;
593-
for (size_t i = 0; i < input.dim() - 1; i++) {
593+
for (ssize_t i = 0; i < input.dim() - 1; i++) {
594594
num_channels *= input.size(i);
595595
}
596596
// This unfortunate change is needed because we compile op_quantize for aten

kernels/quantized/cpu/op_embedding.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ void resize_out_tensor(
200200
const Tensor& indices,
201201
Tensor& out) {
202202
executorch::aten::SizesType expected_output_size[kTensorDimensionLimit];
203-
for (size_t i = 0; i < indices.dim(); i++) {
203+
for (ssize_t i = 0; i < indices.dim(); i++) {
204204
expected_output_size[i] = indices.size(i);
205205
}
206206
const size_t embedding_dim = weight.size(1);

kernels/quantized/cpu/op_quantize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ Tensor& quantize_per_token_out(
642642
ScalarType dtype,
643643
Tensor& out) {
644644
size_t num_tokens = 1;
645-
for (size_t i = 0; i < input.dim() - 1; i++) {
645+
for (ssize_t i = 0; i < input.dim() - 1; i++) {
646646
num_tokens *= input.size(i);
647647
}
648648
// This unfortunate change is needed because we compile op_quantize for aten

kernels/test/custom_kernel_example/op_relu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void relu(const Tensor& input, Tensor& output) {
3838
CTYPE* out_data = output.data_ptr<CTYPE>();
3939
size_t lim = input.numel();
4040
Tensor::SizesType expected_output_size[16];
41-
for (size_t i = 0; i < output.dim(); ++i) {
41+
for (ssize_t i = 0; i < output.dim(); ++i) {
4242
expected_output_size[i] = input.size(i);
4343
}
4444
auto error = resize_tensor(

kernels/test/op_split_copy_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ TEST_F(OpSplitCopyTensorOutTest, LargerSplitSizeDoesNothing) {
288288
std::vector<Tensor> expected_out = {input};
289289

290290
for (int64_t split_size = 3; split_size < 6; ++split_size) {
291-
for (size_t dim = 0; dim < input.dim(); ++dim) {
291+
for (ssize_t dim = 0; dim < input.dim(); ++dim) {
292292
TensorList out = tlf.zeros_like({input});
293293
op_split_copy_tensor_out(input, split_size, dim, out);
294294
EXPECT_TENSOR_LISTS_EQ(out, expected_out);

0 commit comments

Comments
 (0)