tf.stridedSlice returns wrong values or throws when shrinkAxisMask is combined with a non-unit stride on another axis

未关闭
#8,756 1 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

@nileshau-afk 已经在做这个了。

开始于 2026年11月9日。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
typescript

调研方向

Start with tfjs-backend-cpu/src/kernels/StridedSlice.ts and tfjs-core/src/ops/slice_util.ts, where the issue points to a rank mismatch between the processing shape and the stride and begin arrays. Check the existing strided-slice tests and reproduce both examples on the CPU and WASM backends. Done means both slices return [1, 5] and [3, 5] on both backends without an out-of-range error.

由索引模型根据 Issue 内容生成。

描述

comp:backend-cpu comp:core stat:awaiting response type:bug

Please make sure that this is a bug. As per our
GitHub Policy,
we only address code/doc bugs, performance issues, feature requests and
build/installation issues on GitHub. tag:bug_template

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow.js): yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux x86_64 (Debian 12)
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: n/a
  • TensorFlow.js installed from (npm or script link): npm
  • TensorFlow.js version (use command below): @tensorflow/tfjs-core, @tensorflow/tfjs-backend-cpu, @tensorflow/tfjs-backend-wasm 4.22.0 (master 7f5309fe has no changes to the files involved), Node.js 26.13.0
  • Browser version: n/a
  • Tensorflow.js Converter Version: n/a

Describe the current behavior

When one axis is shrunk (shrinkAxisMask) and another axis has a stride other than 1, tf.stridedSlice reads the wrong elements on the cpu backend or throws Requested out of range element. The wasm backend returns different, also wrong, values. These are the slices Python writes as y[::2, 1] or x[1, ::2], so converted models that contain such a StridedSlice op are affected too.

Describe the expected behavior

Same result as tf.strided_slice in TensorFlow: [1, 5] and [3, 5].

Standalone code to reproduce the issue

const tf = require("@tensorflow/tfjs-core");
require("@tensorflow/tfjs-backend-cpu");
require("@tensorflow/tfjs-backend-wasm");
(async () => {
  await tf.setBackend(process.argv[2]);
  const y = tf.tensor2d([0, 1, 2, 3, 4, 5], [3, 2]);
  // y[::2, 1] -> expected [1, 5]
  console.log(Array.from(tf.stridedSlice(y, [0, 1], [0, 2], [2, 1], 1, 1, 0, 0, 2).dataSync()));
  const x = tf.tensor2d([0, 1, 2, 3, 4, 5], [2, 3]);
  // x[1, ::2] -> expected [3, 5]
  try { console.log(Array.from(tf.stridedSlice(x, [1, 0], [2, 0], [1, 2], 0, 2, 0, 0, 1).dataSync())); } catch (e) { console.log("Error: " + e.message); }
})();
$ node ss.js cpu
[ 0, 2 ]
Error: Requested out of range element at 2.   Buffer shape=2,3
$ node ss.js wasm
[ 0, 2 ]
[ 1, 2 ]

TensorFlow 2.21 (y[::2, 1], x[1, ::2], or tf.strided_slice with the same masks) gives [1. 5.] and [3. 5.].

Other info / logs

On cpu the general path calls stridedSliceImpl(finalShapeSparse, xBuf, $strides, $begin). finalShapeSparse has the shrunk axes removed, while $begin and $strides are per dense axis, so the location arrays have the wrong rank (tfjs-backend-cpu/src/kernels/StridedSlice.ts, tfjs-core/src/ops/slice_util.ts sliceInfo). Using the processing shape (shrunk axes kept as size 1) there and reshaping to finalShape afterwards should fix it.

Credit: https://ucsbplse.github.io/

主要语言
TypeScript
星标
19.2k
派生
2k
PR 合并指标
30 天内没有已合并 PR

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

tensorflow/tfjs 的其他 Issue

查看 tensorflow/tfjs 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。