Skip to content

Commit 3dfa644

Browse files
Fix out-of-bounds traversal in reduce_util for non-contiguous tensors (pytorch#21517)
### Summary Fixes pytorch#16429. `dequantize_per_channel` on a channels-last input writes past the end of its output. The reported symptom is wrong values, but the underlying problem is a memory error and it is not in the quantized kernel. `apply_on_flat_ix_with_dim_mask_and_base` in `kernels/portable/cpu/util/reduce_util.h` walks the elements that map to one output index. When the innermost dim is exhausted it rewinds `curr_index` and carries into the next dim, using: ```cpp curr_index -= strides[curr_dim - 1]; ``` The comment above that line states the assumption: it treats `strides[curr_dim - 1]` as equal to `in.size(curr_dim) * strides[curr_dim]`. That identity only holds for a contiguous tensor. For a `(2, 2, 3, 3)` channels-last tensor, strides are `[18, 1, 6, 2]`, and the two disagree at `curr_dim = 1` (2 vs 18) and `curr_dim = 2` (18 vs 1). The rewind is then wrong and the walk leaves the buffer. For `dequantize_per_channel` on that tensor the walk covers indices 0 to 17, skips 18 to 34 entirely, and writes 17 times past the end of a 36 element output. Running it under the existing kernel tests produces wrong values followed by `double free or corruption (!prev)`. This changes the line to subtract the value the comment describes. For a contiguous tensor the two expressions evaluate to the same number, so that path is unaffected, and it is in the carry-over rather than the inner loop, so it is not on the per-element path. Most reduction ops do not reach this. `op_amax`, `op_amin`, `op_mean`, `op_sum` and `op_var` all guard with `tensor_is_default_dim_order(in)` and reject non-contiguous input before the traversal runs. The ops that accept it are `op_any`, `op_var_mean` and `dequantize_per_channel`, and I have confirmed the fault in two of them: `dequantize_per_channel` writes out of bounds, and `any.dims_out` reads out of bounds and returns a wrong answer. I took this over adding a `tensor_is_default_dim_order` guard to `dequantize_per_channel` to match the other five ops. A guard would stop the corruption but leaves the traversal wrong for `op_any` and `op_var_mean`, and pytorch#16429 asks for non-contiguous dim order to be supported in portable ops rather than rejected. Happy to switch if you would rather have the guard. Relaxing the five existing guards is not part of this change. ### Test plan `OpDequantizeOutTest.DequantizePerChannelChannelsLast` dequantizes a `(2, 2, 3, 3)` channels-last int8 tensor per channel on `axis=1`. Each expected value is derived from the element's channel alone, so it passes only if the kernel honors the tensor's dim order. `OpAnyOutTest.ChannelsLastMultiDimReduction` reduces a channels-last tensor over dims `{0, 2, 3}` with `keepdim=true`. The single true value sits at channels-last physical index 19, inside the range the old traversal skipped, so before this change channel 1 was reported false while channel 0 picked up out-of-bounds memory and was reported true. Both fail before this change and pass after it. This also adds `op_dequantize_test.cpp` to `_quantized_kernels_test_sources` in `kernels/test/CMakeLists.txt`. It is already in `kernels/quantized/test/targets.bzl`, but the CMake target lists every other op in that directory and omits this one, so those tests do not run in a CMake build and the new one would not either. Full runs after the change: `quantized_kernels_test` 73 passed, `portable_kernels_test` 1624 ran with 1520 passed and 0 failures. cc @larryliu0820 @manuelcandales @JakeStevens --------- Co-authored-by: Jacob Szwejbka <jakeszwe@meta.com>
1 parent 52bbb30 commit 3dfa644

5 files changed

Lines changed: 119 additions & 4 deletions

File tree

‎kernels/portable/cpu/util/reduce_util.h‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,10 @@ void apply_on_flat_ix_with_dim_mask_and_base(
107107
// Reset dim_index[curr_dim] to 0. We need to update curr_index
108108
// accordingly. Reseting dim_index[curr_dim] from in.size(curr_dim)
109109
// to 0 means we need to subtract in.size(curr_dim) * strides[curr_dim]
110-
// from curr_index. However in.size(curr_dim) * strides[curr_dim] is
111-
// equal to strides[curr_dim - 1]. Notice that curr_dim > 0 at this
112-
// point in the execution
110+
// from curr_index. This is only equal to strides[curr_dim - 1] for a
111+
// contiguous memory format, so it cannot be simplified to that.
113112
dim_index[curr_dim] = 0;
114-
curr_index -= strides[curr_dim - 1];
113+
curr_index -= in.size(curr_dim) * strides[curr_dim];
115114

116115
// Decrease current dim
117116
curr_dim--;

‎kernels/portable/cpu/util/test/reduce_test.cpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,44 @@ TEST(ReduceUtilTest, ApplyOverDimListLength4) {
432432
EXPECT_TENSOR_EQ(in, tf.zeros({2, 4, 5, 3}));
433433
}
434434

435+
TEST(ReduceUtilTest, ApplyOverDimListChannelsLast) {
436+
TensorFactory<ScalarType::Long> tf;
437+
optional<ArrayRef<int64_t>> dim_list;
438+
439+
// Reducing over every dim but 1 should visit each element exactly once.
440+
// Doing that requires following the tensor's strides, which the traversal
441+
// did not do for a channels-last memory format: it covered part of the
442+
// tensor twice and ran off the end of the buffer. The indices are counted
443+
// here rather than written through, so an out-of-bounds index is reported
444+
// instead of corrupting memory.
445+
Tensor in = tf.zeros_channels_last({2, 2, 3, 3});
446+
int64_t dim_array_023[3] = {0, 2, 3};
447+
dim_list = optional<ArrayRef<int64_t>>(ArrayRef<int64_t>{dim_array_023, 3});
448+
449+
const size_t numel = static_cast<size_t>(in.numel());
450+
std::vector<size_t> visits(numel, 0);
451+
size_t out_of_bounds = 0;
452+
for (size_t out_ix = 0; out_ix < get_out_numel(in, dim_list); ++out_ix) {
453+
apply_over_dim_list(
454+
[&visits, &out_of_bounds, numel](size_t in_ix) {
455+
if (in_ix >= numel) {
456+
++out_of_bounds;
457+
} else {
458+
++visits[in_ix];
459+
}
460+
},
461+
in,
462+
dim_list,
463+
out_ix);
464+
}
465+
466+
EXPECT_EQ(out_of_bounds, 0u);
467+
for (size_t i = 0; i < numel; ++i) {
468+
EXPECT_EQ(visits[i], 1u) << "element " << i << " visited " << visits[i]
469+
<< " times, expected exactly once";
470+
}
471+
}
472+
435473
TEST(ReduceUtilTest, ApplyOnZeroDimTensorOverDim) {
436474
TensorFactory<ScalarType::Long> tf;
437475

‎kernels/quantized/test/op_dequantize_test.cpp‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,54 @@ TEST(OpDequantizeOutTest, DequantizePerChannel) {
317317
test_per_channel_dtype<ScalarType::Byte>();
318318
test_per_channel_dtype<ScalarType::Char>();
319319
}
320+
321+
// Per-channel dequantize on a channels-last input. Each element's expected
322+
// value depends only on its channel, never on where it sits in memory, so this
323+
// only passes if the kernel honors the tensor's dim order. Before the
324+
// reduce_util.h carry-over fix this wrote past the end of the output. See
325+
// issue #16429.
326+
TEST(OpDequantizeOutTest, DequantizePerChannelChannelsLast) {
327+
et_pal_init();
328+
TensorFactory<ScalarType::Char> tf;
329+
TensorFactory<ScalarType::Double> tf_double;
330+
TensorFactory<ScalarType::Long> tf_long;
331+
TensorFactory<ScalarType::Float> tfo;
332+
333+
std::vector<int8_t> logical = {
334+
-20, -59, -22, -40, 127, -108, -57, 117, 24, -103, 48, -110,
335+
80, 15, -10, -75, -77, -46, -12, -66, 35, -87, -50, -80,
336+
12, -127, 107, 91, 115, -54, -6, -6, -41, 46, 42, -83};
337+
const double s0 = 0.0016989057185128331;
338+
const double s1 = 0.001776964869350195;
339+
340+
Tensor input = tf.channels_last_like(tf.make({2, 2, 3, 3}, logical));
341+
Tensor scale = tf_double.make({2}, {s0, s1});
342+
Tensor zero_point = tf_long.make({2}, {0, 0});
343+
344+
std::vector<float> expected_logical(36);
345+
for (int n = 0; n < 2; ++n) {
346+
for (int c = 0; c < 2; ++c) {
347+
for (int hw = 0; hw < 9; ++hw) {
348+
size_t i = n * 18 + c * 9 + hw;
349+
expected_logical[i] = static_cast<float>(logical[i]) *
350+
static_cast<float>(c == 0 ? s0 : s1);
351+
}
352+
}
353+
}
354+
Tensor expected =
355+
tfo.channels_last_like(tfo.make({2, 2, 3, 3}, expected_logical));
356+
357+
Tensor out = tfo.zeros_channels_last({2, 2, 3, 3});
358+
dequantize_per_channel_out(
359+
input,
360+
scale,
361+
zero_point,
362+
/*axis=*/1,
363+
/*quant_min=*/-128,
364+
/*quant_max=*/127,
365+
ScalarType::Char,
366+
optional<ScalarType>(),
367+
out);
368+
369+
EXPECT_TENSOR_CLOSE(out, expected);
370+
}

‎kernels/test/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ if(TARGET quantized_kernels)
411411
set(_quantized_kernels_test_sources
412412
"${EXECUTORCH_ROOT}/kernels/quantized/test/op_add_test.cpp"
413413
"${EXECUTORCH_ROOT}/kernels/quantized/test/op_choose_qparams_test.cpp"
414+
"${EXECUTORCH_ROOT}/kernels/quantized/test/op_dequantize_test.cpp"
414415
"${EXECUTORCH_ROOT}/kernels/quantized/test/op_embedding2b_test.cpp"
415416
"${EXECUTORCH_ROOT}/kernels/quantized/test/op_embedding4b_test.cpp"
416417
"${EXECUTORCH_ROOT}/kernels/quantized/test/op_embedding_test.cpp"

‎kernels/test/op_any_test.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,29 @@ TEST_F(OpAnyOutTest, TestAnyDimsOutEmptyDimList) {
208208
op_any_dims_out(self, opt_dim_list, keepdim, out);
209209
EXPECT_TENSOR_CLOSE(out, out_expected);
210210
}
211+
212+
// Reducing over more than one dim walks the shared dim-mask traversal in
213+
// reduce_util.h. Its carry-over used to assume
214+
// size(d) * stride(d) == stride(d - 1), which only holds for a contiguous
215+
// tensor, so on a channels-last input it skipped a block of elements and read
216+
// past the end of the buffer. See issue #16429.
217+
//
218+
// The single true value below sits at channels-last physical index 19, inside
219+
// the range the broken traversal never visited, so channel 1 was reported as
220+
// false while channel 0 picked up out-of-bounds memory and was reported true.
221+
TEST_F(OpAnyOutTest, ChannelsLastMultiDimReduction) {
222+
TensorFactory<ScalarType::Float> tf;
223+
TensorFactory<ScalarType::Bool> tf_bool;
224+
225+
std::vector<float> data(36, 0.0f);
226+
data[27] = 1.0f; // logical (n=1, c=1, h=0, w=0)
227+
Tensor in = tf.channels_last_like(tf.make({2, 2, 3, 3}, data));
228+
229+
Tensor out = tf_bool.zeros_channels_last({1, 2, 1, 1});
230+
int64_t dims[3] = {0, 2, 3};
231+
op_any_dims_out(in, ArrayRef<int64_t>{dims, 3}, /*keepdim=*/true, out);
232+
233+
Tensor expected =
234+
tf_bool.channels_last_like(tf_bool.make({1, 2, 1, 1}, {false, true}));
235+
EXPECT_TENSOR_EQ(out, expected);
236+
}

0 commit comments

Comments
 (0)