← Back to Pull requests

Fix expression slices after dropping a dimension #1254

Open
Arthur031221 opened this pull request with 163 lines diff across 2 files
View on GitHub
Arthur031221 Arthur031221 commented on Oct 02, 2026 05:01 UTC
Author

Users slicing an expression after dropping an earlier dimension get values from the wrong offset. For a fixed 3 by 5 input, slice<1>(input + 0, {1, 2}, {matxDropDim, matxEnd}) returned 11, 12, 13 instead of 12, 13, 14. Adding a stride to this expression failed to compile.

SliceOp read start offsets and calculated strided output sizes using an output dimension index where it needed an input dimension index. Its strided overload declared a reference member that it could not initialize. The fix stores strides by value and includes them in JIT argument storage, which the generated operator reads.

The new test checks literal values for ordinary and strided expression slices, the downsample wrapper on an expression, a tensor-view control, and JIT execution of both slice expressions. From the worktree, build/test/test_00_operators_slice_test --gtest_brief=1 passed 49 of 49 tests and build-jit/test/test_00_operators_slice_test --gtest_brief=1 passed 61 of 61 tests on an RTX 5090 with CUDA 13.2.51. The slice-stride, slice-and-reduce, slice-and-reshape, and up/downsample targets passed 16 of 16, 48 of 48, 8 of 8, and 96 of 96 tests. The full test suite was not built.

Diff: 163 lines (+152 / -11) across 2 files Reviewer: Arthur031221 Review duration: 30973m
All Vouch review confidence checks passed
Residual risk 0.09 = Change Risk (0.49) × (1 − Review Confidence (0.81)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.49 · 163 lines diff across 2 files
0.49
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.92 · 5 substantive comment(s)
0.92
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
80%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.305
Self Review self_review
0.140
Real PR Conversation Timeline
22 items
copy-pr-bot[bot] copy-pr-bot[bot] commented on Oct 02, 2026 05:02 UTC
Bot

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

greptile-apps[bot] greptile-apps[bot] commented on Oct 02, 2026 05:04 UTC
Bot

<!-- greptile_summary -->

<h2><a href="https://app.greptile.com/api/retrigger?id=73304924"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img alt="Retrigger" src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2" align="right"></picture></a><a href="https://app.greptile.com/nvidia-public-github/github/NVIDIA/matx/-/pull/1254"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptileDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"><img alt="View in Greptile" src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2" align="right"></picture></a>Confidence Score: 5/5</h2>

[Medium impact] The PR appears safe to merge, with the existing non-blocking concern about extra JIT compilations for moving slice windows.

<h2>Findings</h2>

1. <img alt="P2" src="https://greptile-static-assets.s3.amazonaws.com/badges/p2.svg?v=9" align="top">&nbsp;Moving slices lose kernel reuse <a href="https://github.com/NVIDIA/MatX/pull/1254#discussion_r4171666323">▶</a>

<h3>Summary</h3>

Fixes expression slices that drop an earlier dimension.

  • Expression slices keep the right starting point after dropping a dimension.
  • No code changed since the previous review. No new findings were identified.

    cliffburdick dismissed the old-cache concern because the change had not been merged or released; Greptile agreed.

    <!-- greptile_confidence_score:5 -->

    <sub>Reviews (6) · Last reviewed commit: "Fix expression slices after dropping a d..." · Reviewed by Greptile</sub>

    greptile-apps[bot] greptile-apps[bot] commented on code on Oct 02, 2026 05:04 UTC
    Code Review
    include/matx/operators/slice.h:1
    @@ -124,10 +125,10 @@ namespace matx
                     "        for(int32_t j = 0; j < DIM_; j++) {{\n"
                     "          if(dims_[j] == i) {{\n"
                     "            if constexpr (!cuda::std::is_same_v<NoStride, StrideType>) {{\n"
    -                "              ind[i] = starts_[j] + inds[j] * strides_[i];\n"
    +                "              ind[i] = starts_[i] + inds[j] * strides_[i];\n"
                     "            }}\n"
                     "            else {{\n"
    -                "              ind[i] = starts_[j] + inds[j];\n"
    +                "              ind[i] = starts_[i] + inds[j];\n"

    <a href="#"><img alt="P1" src="https://greptile-static-assets.s3.amazonaws.com/badges/p1.svg?v=9" align="top"></a> Old JIT kernels remain cached

    If a JIT-enabled application has a disk-cached slice kernel from before this change, it can load that kernel instead of compiling the corrected indexing code. For a slice that drops a leading dimension, the cached kernel still uses the old start-offset mapping and returns the wrong values. The slice's cache identity needs to change with the generated code.

    Knowledge Base Used: CUDA and JIT executors

    greptile-apps[bot] greptile-apps[bot] commented on code on Oct 02, 2026 05:04 UTC
    Code Review
    include/matx/operators/slice.h:1
    @@ -124,10 +125,10 @@ namespace matx
                     "        for(int32_t j = 0; j < DIM_; j++) {{\n"
                     "          if(dims_[j] == i) {{\n"
                     "            if constexpr (!cuda::std::is_same_v<NoStride, StrideType>) {{\n"
    -                "              ind[i] = starts_[j] + inds[j] * strides_[i];\n"
    +                "              ind[i] = starts_[i] + inds[j] * strides_[i];\n"

    <a href="#"><img alt="P1" src="https://greptile-static-assets.s3.amazonaws.com/badges/p1.svg?v=9" align="top"></a> Different starts reuse JIT kernel

    When two JIT slices have the same output dimensions and sizes but different starts, they share a cache identity even though the generated kernel embeds the start offsets. For example, strided slices starting at {1, 1} and {1, 2} can both have length two. The second slice can then run the first slice's kernel and read from the wrong offset.

    Knowledge Base Used:

  • Indexing and shape operators

  • CUDA and JIT executors
  • greptile-apps[bot] greptile-apps[bot] commented on code on Oct 02, 2026 05:04 UTC
    Code Review
    test/00_operators/slice_test.cu:1
    @@ -6,6 +6,59 @@
     using namespace matx;
     using namespace matx::test;
     
    +TEST(SliceOpRegression, DroppedLeadingDimensionKeepsRemainingStart)
    +{
    +  auto input = make_tensor<int>({3, 5});
    +  for (index_t row = 0; row < 3; ++row) {
    +    for (index_t column = 0; column < 5; ++column) {
    +      input(row, column) = static_cast<int>(10 * row + column);
    +    }
    +  }
    +
    +  auto view = slice<1>(input, {1, 2}, {matxDropDim, matxEnd});
    +  auto expression = slice<1>(input + 0, {1, 2}, {matxDropDim, matxEnd});
    +  ASSERT_EQ(expression.Size(0), 3);
    +  for (index_t column = 0; column < expression.Size(0); ++column) {
    +    const int expected = static_cast<int>(12 + column);
    +    EXPECT_EQ(view(column), expected);
    +    EXPECT_EQ(expression(column), expected);
    +  }
    +
    +  auto strided_view = slice<1>(input, {1, 2}, {matxDropDim, matxEnd}, {1, 2});
    +  auto strided_expression = slice<1>(input + 0, {1, 2}, {matxDropDim, matxEnd}, {1, 2});
    +  ASSERT_EQ(strided_expression.Size(0), 2);
    +  for (index_t column = 0; column < strided_expression.Size(0); ++column) {
    +    const int expected = static_cast<int>(12 + 2 * column);
    +    EXPECT_EQ(strided_view(column), expected);
    +    EXPECT_EQ(strided_expression(column), expected);
    +  }
    +
    +  auto downsampled = downsample(input + 0, 1, 2);
    +  ASSERT_EQ(downsampled.Size(1), 3);
    +  for (index_t row = 0; row < 3; ++row) {
    +    for (index_t column = 0; column < downsampled.Size(1); ++column) {
    +      EXPECT_EQ(downsampled(row, column), 10 * row + 2 * column);
    +    }
    +  }
    +
    +#ifdef MATX_EN_JIT
    +  CUDAJITExecutor exec{};
    +  auto jit_output = make_tensor<int>({3});
    +  (jit_output = expression).run(exec);

    <a href="#"><img alt="P2" src="https://greptile-static-assets.s3.amazonaws.com/badges/p2.svg?v=9" align="top"></a> Regular CUDA path lacks coverage

    The new ordinary and strided expression checks read values on the host; the only new device assignments use CUDAJITExecutor. A regression in the regular cudaExecutor path for a dropped-dimension slice would therefore pass this focused test. Adding a regular CUDA assignment and value check would cover that path.

    Knowledge Base Used: Validation and continuous integration

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

    Arthur031221 Arthur031221 commented on code on Oct 02, 2026 07:27 UTC
    Code Review
    include/matx/operators/slice.h:1
    @@ -124,10 +125,10 @@ namespace matx
                     "        for(int32_t j = 0; j < DIM_; j++) {{\n"
                     "          if(dims_[j] == i) {{\n"
                     "            if constexpr (!cuda::std::is_same_v<NoStride, StrideType>) {{\n"
    -                "              ind[i] = starts_[j] + inds[j] * strides_[i];\n"
    +                "              ind[i] = starts_[i] + inds[j] * strides_[i];\n"
                     "            }}\n"
                     "            else {{\n"
    -                "              ind[i] = starts_[j] + inds[j];\n"
    +                "              ind[i] = starts_[i] + inds[j];\n"

    Versioned the generated slice class as JITSlice_v3 so its cache identity excludes the old class names. Both focused regressions passed using the existing pre-fix disk cache, and all 27 existing cache files remained byte-identical.

    Arthur031221 Arthur031221 commented on code on Oct 02, 2026 07:27 UTC
    Code Review
    include/matx/operators/slice.h:1
    @@ -124,10 +125,10 @@ namespace matx
                     "        for(int32_t j = 0; j < DIM_; j++) {{\n"
                     "          if(dims_[j] == i) {{\n"
                     "            if constexpr (!cuda::std::is_same_v<NoStride, StrideType>) {{\n"
    -                "              ind[i] = starts_[j] + inds[j] * strides_[i];\n"
    +                "              ind[i] = starts_[i] + inds[j] * strides_[i];\n"

    Moved resolved starts into runtime JIT storage. In a fresh-cache probe, starts {1,1}, {1,2}, {2,1}, and {1,1} passed all eight value assertions while reusing one cached kernel. The regression exercises starts on retained and dropped axes for ordinary and strided slices.

    Arthur031221 Arthur031221 commented on code on Oct 02, 2026 07:27 UTC
    Code Review
    test/00_operators/slice_test.cu:1
    @@ -6,6 +6,59 @@
     using namespace matx;
     using namespace matx::test;
     
    +TEST(SliceOpRegression, DroppedLeadingDimensionKeepsRemainingStart)
    +{
    +  auto input = make_tensor<int>({3, 5});
    +  for (index_t row = 0; row < 3; ++row) {
    +    for (index_t column = 0; column < 5; ++column) {
    +      input(row, column) = static_cast<int>(10 * row + column);
    +    }
    +  }
    +
    +  auto view = slice<1>(input, {1, 2}, {matxDropDim, matxEnd});
    +  auto expression = slice<1>(input + 0, {1, 2}, {matxDropDim, matxEnd});
    +  ASSERT_EQ(expression.Size(0), 3);
    +  for (index_t column = 0; column < expression.Size(0); ++column) {
    +    const int expected = static_cast<int>(12 + column);
    +    EXPECT_EQ(view(column), expected);
    +    EXPECT_EQ(expression(column), expected);
    +  }
    +
    +  auto strided_view = slice<1>(input, {1, 2}, {matxDropDim, matxEnd}, {1, 2});
    +  auto strided_expression = slice<1>(input + 0, {1, 2}, {matxDropDim, matxEnd}, {1, 2});
    +  ASSERT_EQ(strided_expression.Size(0), 2);
    +  for (index_t column = 0; column < strided_expression.Size(0); ++column) {
    +    const int expected = static_cast<int>(12 + 2 * column);
    +    EXPECT_EQ(strided_view(column), expected);
    +    EXPECT_EQ(strided_expression(column), expected);
    +  }
    +
    +  auto downsampled = downsample(input + 0, 1, 2);
    +  ASSERT_EQ(downsampled.Size(1), 3);
    +  for (index_t row = 0; row < 3; ++row) {
    +    for (index_t column = 0; column < downsampled.Size(1); ++column) {
    +      EXPECT_EQ(downsampled(row, column), 10 * row + 2 * column);
    +    }
    +  }
    +
    +#ifdef MATX_EN_JIT
    +  CUDAJITExecutor exec{};
    +  auto jit_output = make_tensor<int>({3});
    +  (jit_output = expression).run(exec);

    Added cudaExecutor assignments and value checks for ordinary and strided slices that drop a leading dimension. Changing only the ordinary CUDA device indexing from starts[i] to starts[j] made the five new assertions fail. Restoring the committed header made both focused tests pass. The local JIT-enabled slice target passed 62 tests.

    cliffburdick cliffburdick commented on Oct 02, 2026 18:12 UTC
    Contributor

    /build

    cliffburdick cliffburdick commented on code on Oct 02, 2026 19:08 UTC
    Code Review
    include/matx/operators/slice.h:1
    @@ -74,18 +74,21 @@ namespace matx
     #ifdef MATX_EN_JIT
             struct JIT_Storage {
               typename detail::inner_storage_or_self_t<detail::base_type_t<T>> op_;
    +          cuda::std::array<shape_type, T::Rank()> starts_;

    For the JIT path starts and strides are intentionally encoded in the name so they can be compile-time parameters. What's the reason this is runtime now?

    coveralls coveralls commented on Oct 02, 2026 21:31 UTC
    Contributor

    ![Coverage Status](https://coveralls.io/builds/82369939)

    Coverage is 93.762% — Arthur031221:fix-expression-slice-dropped-dimensions into NVIDIA:main. No base build found for NVIDIA:main.

    Arthur031221 Arthur031221 commented on code on Oct 03, 2026 04:17 UTC
    Code Review
    include/matx/operators/slice.h:1
    @@ -74,18 +74,21 @@ namespace matx
     #ifdef MATX_EN_JIT
             struct JIT_Storage {
               typename detail::inner_storage_or_self_t<detail::base_type_t<T>> op_;
    +          cuda::std::array<shape_type, T::Rank()> starts_;

    I moved them to runtime storage to avoid cache collisions. That was a broader change than needed: on the upstream header, the class name records dimensions and sizes but omits starts and strides.

    The generated slice now uses constexpr starts and strides, with their resolved values in the versioned class name. JIT launch storage now contains the nested operator, replacing the storage layout described in the PR body.

    The JIT-enabled slice and slice-stride targets passed 62 and 20 tests locally. The cache regression varies starts on retained and dropped axes and strides for two-element outputs.

    greptile-apps[bot] greptile-apps[bot] commented on code on Oct 03, 2026 04:19 UTC
    Code Review
    include/matx/operators/slice.h:1
    @@ -85,7 +85,17 @@ namespace matx
               for (int i = 0; i < DIM; i++) {
                 params_str += std::format("d{}_s{}_", dims_[i], sizes_[i]);
               }
    -          return std::format("JITSlice_{}", params_str);
    +          const int input_rank = detail::get_dyn_rank(op_);
    +          for (int i = 0; i < input_rank; i++) {
    +            params_str += std::format("b{}_", starts_[i]);
    +            if constexpr (!cuda::std::is_same_v<StrideType, NoStride>) {
    +              params_str += std::format("t{}_", strides_[i]);
    +            }
    +          }

    <a href="#"><img alt="P2" src="https://greptile-static-assets.s3.amazonaws.com/badges/p2.svg?v=9" align="top"></a> Moving slices lose kernel reuse

    When a JIT workload moves a slice window across an expression, each new start or stride changes the generated class name and cache key. Even if the output shape stays the same, each position needs a separate NVRTC compilation and persistent cubin rather than reusing a kernel with runtime offsets. This can slow repeated windowed slicing and grow the disk cache.

    Knowledge Base Used: CUDA and JIT executors

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

    cliffburdick cliffburdick commented on code on Oct 06, 2026 16:01 UTC
    Code Review
    include/matx/operators/slice.h:1
    @@ -74,18 +74,21 @@ namespace matx
     #ifdef MATX_EN_JIT
             struct JIT_Storage {
               typename detail::inner_storage_or_self_t<detail::base_type_t<T>> op_;
    +          cuda::std::array<shape_type, T::Rank()> starts_;

    Can you explain the part about cache collisions? What's the reason it couldn't stay unique for a different type? Was the name just missing something?

    Arthur031221 Arthur031221 commented on code on Oct 07, 2026 04:28 UTC
    Code Review
    include/matx/operators/slice.h:1
    @@ -74,18 +74,21 @@ namespace matx
     #ifdef MATX_EN_JIT
             struct JIT_Storage {
               typename detail::inner_storage_or_self_t<detail::base_type_t<T>> op_;
    +          cuda::std::array<shape_type, T::Rank()> starts_;

    Yes, the old name omitted start values. I reran a probe on the pre-PR header with starts {1,1} and {1,2} and equal two-element outputs: the expressions have the same C++ type and full JIT type string, and both use JITSlice_d1_s2_, while their generated definitions embed different starts. The current head includes resolved starts and strides in the name and uses constexpr parameters, so moving starts to runtime storage was unnecessary.

    cliffburdick cliffburdick commented on code on Oct 07, 2026 20:27 UTC
    Code Review
    include/matx/operators/slice.h:1
    @@ -124,10 +125,10 @@ namespace matx
                     "        for(int32_t j = 0; j < DIM_; j++) {{\n"
                     "          if(dims_[j] == i) {{\n"
                     "            if constexpr (!cuda::std::is_same_v<NoStride, StrideType>) {{\n"
    -                "              ind[i] = starts_[j] + inds[j] * strides_[i];\n"
    +                "              ind[i] = starts_[i] + inds[j] * strides_[i];\n"
                     "            }}\n"
                     "            else {{\n"
    -                "              ind[i] = starts_[j] + inds[j];\n"
    +                "              ind[i] = starts_[i] + inds[j];\n"

    I would undo this change. This has not been merged so there's no reason an old version of it would exist.

    greptile-apps[bot] greptile-apps[bot] commented on code on Oct 07, 2026 20:27 UTC
    Code Review
    include/matx/operators/slice.h:1
    @@ -124,10 +125,10 @@ namespace matx
                     "        for(int32_t j = 0; j < DIM_; j++) {{\n"
                     "          if(dims_[j] == i) {{\n"
                     "            if constexpr (!cuda::std::is_same_v<NoStride, StrideType>) {{\n"
    -                "              ind[i] = starts_[j] + inds[j] * strides_[i];\n"
    +                "              ind[i] = starts_[i] + inds[j] * strides_[i];\n"
                     "            }}\n"
                     "            else {{\n"
    -                "              ind[i] = starts_[j] + inds[j];\n"
    +                "              ind[i] = starts_[i] + inds[j];\n"

    That’s fair. Since this change has not been merged or released, there is no supported consumer expected to have a cache from an earlier version of this operator, so changing the JIT class name is unnecessary. The pre-fix cache used in the local regression is a test-environment artifact rather than a compatibility concern. I agree this versioning change should be reverted while keeping the functional indexing fix.

    Arthur031221 Arthur031221 commented on code on Oct 07, 2026 21:04 UTC
    Code Review
    include/matx/operators/slice.h:1
    @@ -124,10 +125,10 @@ namespace matx
                     "        for(int32_t j = 0; j < DIM_; j++) {{\n"
                     "          if(dims_[j] == i) {{\n"
                     "            if constexpr (!cuda::std::is_same_v<NoStride, StrideType>) {{\n"
    -                "              ind[i] = starts_[j] + inds[j] * strides_[i];\n"
    +                "              ind[i] = starts_[i] + inds[j] * strides_[i];\n"
                     "            }}\n"
                     "            else {{\n"
    -                "              ind[i] = starts_[j] + inds[j];\n"
    +                "              ind[i] = starts_[i] + inds[j];\n"

    I removed the version tag from the JIT slice class name. For slice<1>(input + 0, {1, 1}, {matxDropDim, 3}) on a 3 by 7 input, the name is now JITSlice_r2_d1_s2_b1_b1_n_. It still records the input rank and the start values, plus the stride values when a stride is given.

    Tested the JIT-enabled slice, slice-stride, slice-and-reduce, slice-and-reshape and up/downsample test targets. They pass with 62, 20, 60, 10 and 120 tests.

    cliffburdick cliffburdick commented on code on Oct 07, 2026 21:39 UTC
    Code Review
    include/matx/operators/slice.h:1
    @@ -74,18 +74,21 @@ namespace matx
     #ifdef MATX_EN_JIT
             struct JIT_Storage {
               typename detail::inner_storage_or_self_t<detail::base_type_t<T>> op_;
    +          cuda::std::array<shape_type, T::Rank()> starts_;

    shouldn't we part the start offsets as part of the string then?

    Arthur031221 Arthur031221 commented on code on Oct 08, 2026 00:37 UTC
    Code Review
    include/matx/operators/slice.h:1
    @@ -74,18 +74,21 @@ namespace matx
     #ifdef MATX_EN_JIT
             struct JIT_Storage {
               typename detail::inner_storage_or_self_t<detail::base_type_t<T>> op_;
    +          cuda::std::array<shape_type, T::Rank()> starts_;

    JIT_Storage holds only op_ again and the class name carries the start values. slice<1>(input + 0, {1, 1}, {matxDropDim, 3}) and slice<1>(input + 0, {1, 2}, {matxDropDim, 4}) on a 3 by 7 int input have the same C++ type but get JITSlice_r2_d1_s2_b1_b1_n_ and JITSlice_r2_d1_s2_b1_b2_n_. The generated source has constexpr starts_ = { 1, 1 } in one and starts_ = { 1, 2 } in the other.

    Strides are in the name too. With strides {1, 1} and {1, 2} (ends 3 and 4, so both give two elements) the names are JITSlice_r2_d1_s2_b1_t1_b1_t1_ and JITSlice_r2_d1_s2_b1_t1_b1_t2_.

    cliffburdick cliffburdick commented on Oct 14, 2026 18:16 UTC
    Contributor

    /build

    Reviewers
    Assignees
    Arthur031221 Arthur031221
    Labels
    risk: low confidence: 80.6% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 49.0% (MEDIUM) · Higher = larger blast radius
    49.0%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 80.6% · Lower = rushed or shallow review
    80.6%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 9.5% · Safe to merge
    9.5%
    Repository