slice#
Slice an operator with new start and end points, and optionally new strides. Slice can also be used to drop ranks, for operations such as selecting a single row. Negative indices can be used to indicate starting at the end and going backward.
When slicing along any given tensor dimension, the start index is treated as inclusive, and the end index as exclusive.
Start, end, and stride arrays always refer to the input dimensions, including dimensions removed with
matxDropDim. For example, for an input whose values are 10 * row + column,
slice<1>(input + 0, {1, 2}, {matxDropDim, matxEnd}) selects row 1 starting at column 2.
For a 3 by 5 input this produces 12, 13, 14; adding strides {1, 2} produces 12, 14.
With the CUDA JIT executor (MATX_EN_JIT=ON), expression slice starts and strides are compiled
into the generated kernel. Different resolved starts or strides therefore require separate JIT
kernel specializations, even when the output shapes match.
Added in version 0.3.0.
-
auto matx::slice(const OpType &op, const index_t (&starts)[OpType::Rank()], const index_t (&ends)[OpType::Rank()], const index_t (&strides)[OpType::Rank()])#
-
auto matx::slice(const OpType &op, const index_t (&starts)[OpType::Rank()], const index_t (&ends)[OpType::Rank()])#
Examples#
auto t2 = make_tensor<TestType>({20, 10});
auto t3 = make_tensor<TestType>({30, 20, 10});
auto t4 = make_tensor<TestType>({40, 30, 20, 10});
(t2 = clone<2>(linspace((inner_type)0, (inner_type)10, 10), {t2.Size(0), matxKeepDim})).run(exec);
(t3 = clone<3>(linspace((inner_type)0, (inner_type)10, 10), {t3.Size(0), t3.Size(1), matxKeepDim})).run(exec);
(t4 = clone<4>(linspace((inner_type)0, (inner_type)10, 10), {t4.Size(0), t4.Size(1), t4.Size(2), matxKeepDim})).run(exec);
exec.sync();
// Slice with different start and end points in each dimension
auto t2t = slice(t2, {1, 2}, {3, 5});
auto t3t = slice(t3, {1, 2, 3}, {3, 5, 7});
auto t4t = slice(t4, {1, 2, 3, 4}, {3, 5, 7, 9});
auto t1 = make_tensor<TestType>({10});
t1.SetVals({10, 20, 30, 40, 50, 60, 70, 80, 90, 100});
// Slice every other element from a 1D tensor (stride of two)
auto t1t = slice(t1, {0}, {matxEnd}, {2});
// Slice "t2t" by selecting column "j" from a 2D operator and converting to a 1D operator
auto t2sly = slice<1>(t2t, {0, j}, {matxEnd, matxDropDim});
auto t2sn = slice(t2, {-4, -5}, {matxEnd, matxEnd});
auto t2s = slice(t2, {t2.Size(0) - 4, t2.Size(1) - 5}, {matxEnd, matxEnd});