Fix expression slices after dropping a dimension #1254
<!-- 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"> 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.
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>
<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
<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:
<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!
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.
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.
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.
/build
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?

Coverage is 93.762% — Arthur031221:fix-expression-slice-dropped-dimensions into NVIDIA:main. No base build found for NVIDIA:main.
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.
<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!
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?
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.
I would undo this change. This has not been merged so there's no reason an old version of it would exist.
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.
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.
shouldn't we part the start offsets as part of the string then?
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_.
/build
Arthur031221
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})returned11, 12, 13instead of12, 13, 14. Adding a stride to this expression failed to compile.SliceOpread 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
downsamplewrapper 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=1passed 49 of 49 tests andbuild-jit/test/test_00_operators_slice_test --gtest_brief=1passed 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.