diff --git a/patches/0005-openmv-Support-any-dim-order-in-block-copy-kernels.patch b/patches/0005-openmv-Support-any-dim-order-in-block-copy-kernels.patch new file mode 100644 index 0000000..c2f5ca4 --- /dev/null +++ b/patches/0005-openmv-Support-any-dim-order-in-block-copy-kernels.patch @@ -0,0 +1,139 @@ +Support any dim order (e.g. channels-last) in the portable kernels that copy +in blocks: cat, split_with_sizes_copy and slice_copy/narrow_copy. Upstream +(pytorch/executorch#21865) rejects non-default dim orders in these kernels. + +diff --git a/kernels/portable/cpu/op_cat.cpp b/kernels/portable/cpu/op_cat.cpp +index 17fdc8f..4ca361b 100644 +--- a/kernels/portable/cpu/op_cat.cpp ++++ b/kernels/portable/cpu/op_cat.cpp +@@ -28,10 +28,6 @@ Tensor& cat_out( + + ET_KERNEL_CHECK(ctx, check_cat_args(tensors, dim, out), InvalidArgument, out); + +- // check_cat_args already requires every input to share out's dim order, so +- // checking out is enough to force all of them to the default. +- ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(out), InvalidArgument, out); +- + Tensor::SizesType expected_out_size[kTensorDimensionLimit]; + size_t expected_out_dim = 0; + ET_KERNEL_CHECK( +@@ -60,8 +56,10 @@ Tensor& cat_out( + return out; + } + +- const size_t outer = getLeadingDims(out, dim); +- const size_t dim_stride = getTrailingDims(out, dim); ++ // OpenMV: check_cat_args requires every input to share out's dim order, so ++ // the blocks are computed in memory order to support any dim order. ++ const size_t outer = getLeadingDimsInMemory(out, dim); ++ const size_t dim_stride = getTrailingDimsInMemory(out, dim); + const size_t ninputs = tensors.size(); + + const auto out_type = out.scalar_type(); +diff --git a/kernels/portable/cpu/op_split_with_sizes_copy.cpp b/kernels/portable/cpu/op_split_with_sizes_copy.cpp +index 0353e04..29ac3cd 100644 +--- a/kernels/portable/cpu/op_split_with_sizes_copy.cpp ++++ b/kernels/portable/cpu/op_split_with_sizes_copy.cpp +@@ -43,8 +43,6 @@ void split_with_sizes_copy_out( + for (const auto i : c10::irange(out.size())) { + ET_KERNEL_CHECK( + ctx, tensors_have_same_dim_order(in, out[i]), InvalidArgument, ); +- ET_KERNEL_CHECK( +- ctx, tensor_is_default_dim_order(out[i]), InvalidArgument, ); + } + + // If out is empty, then nothing needs to be done after checking the args. +@@ -69,8 +67,10 @@ void split_with_sizes_copy_out( + InvalidArgument, ); + } + +- const size_t leading_dims = getLeadingDims(in, dim); +- const size_t trailing_dims = getTrailingDims(in, dim); ++ // OpenMV: the outputs share the input's dim order, so the blocks are ++ // computed in memory order to support any dim order. ++ const size_t leading_dims = getLeadingDimsInMemory(in, dim); ++ const size_t trailing_dims = getTrailingDimsInMemory(in, dim); + const size_t step = in.size(dim) * trailing_dims; + + ScalarType in_type = in.scalar_type(); +diff --git a/kernels/portable/cpu/util/slice_util.cpp b/kernels/portable/cpu/util/slice_util.cpp +index d298297..4c0f89b 100644 +--- a/kernels/portable/cpu/util/slice_util.cpp ++++ b/kernels/portable/cpu/util/slice_util.cpp +@@ -185,8 +185,10 @@ void compute_slice( + /* void */, + "Requested slice is larger than the dim size"); + +- size_t leading_dims = getLeadingDims(in, dim); +- size_t trailing_dims = getTrailingDims(in, dim); ++ // OpenMV: the input and output share a dim order, so the blocks are ++ // computed in memory order to support any dim order. ++ size_t leading_dims = getLeadingDimsInMemory(in, dim); ++ size_t trailing_dims = getTrailingDimsInMemory(in, dim); + + if (trailing_dims == 0) { + return; +diff --git a/runtime/core/exec_aten/util/tensor_util.h b/runtime/core/exec_aten/util/tensor_util.h +index b9cbab4..447ef92 100644 +--- a/runtime/core/exec_aten/util/tensor_util.h ++++ b/runtime/core/exec_aten/util/tensor_util.h +@@ -967,6 +967,50 @@ inline size_t getTrailingDims( + return dims; + } + ++/// OpenMV: Returns the product of the sizes of the dims that come before dim in ++/// the tensor's dim order, i.e. in memory. Equal to getLeadingDims() for the ++/// default dim order. Kernels that copy in blocks use these to support any dim ++/// order (e.g. channels-last) shared by their inputs and outputs. ++inline size_t getLeadingDimsInMemory( ++ const executorch::aten::Tensor& tensor, ++ int64_t dim) { ++ const auto dim_order = tensor.dim_order(); ++ size_t dims = 1; ++ for (size_t i = 0; i < dim_order.size() && dim_order[i] != dim; ++i) { ++ size_t next_dims; ++ ET_CHECK_MSG( ++ !c10::mul_overflows( ++ dims, static_cast(tensor.size(dim_order[i])), &next_dims), ++ "Overflow computing leading dims at dimension %zu", ++ i); ++ dims = next_dims; ++ } ++ return dims; ++} ++ ++/// OpenMV: Returns the product of the sizes of the dims that come after dim in ++/// the tensor's dim order. Equal to getTrailingDims() for the default dim order. ++inline size_t getTrailingDimsInMemory( ++ const executorch::aten::Tensor& tensor, ++ int64_t dim) { ++ const auto dim_order = tensor.dim_order(); ++ size_t i = 0; ++ while (i < dim_order.size() && dim_order[i] != dim) { ++ ++i; ++ } ++ size_t dims = 1; ++ for (++i; i < dim_order.size(); ++i) { ++ size_t next_dims; ++ ET_CHECK_MSG( ++ !c10::mul_overflows( ++ dims, static_cast(tensor.size(dim_order[i])), &next_dims), ++ "Overflow computing trailing dims at dimension %zu", ++ i); ++ dims = next_dims; ++ } ++ return dims; ++} ++ + /** + * Given a N-dimensional tensor coordinate, return a linear index that can be + * used to access the corresponding element in the tensor's data buffer. +@@ -1382,6 +1426,8 @@ using ::executorch::ET_RUNTIME_NAMESPACE::extract_scalar_tensor; + using ::executorch::ET_RUNTIME_NAMESPACE::get_dim_order; + using ::executorch::ET_RUNTIME_NAMESPACE::getLeadingDims; + using ::executorch::ET_RUNTIME_NAMESPACE::getTrailingDims; ++using ::executorch::ET_RUNTIME_NAMESPACE::getLeadingDimsInMemory; ++using ::executorch::ET_RUNTIME_NAMESPACE::getTrailingDimsInMemory; + using ::executorch::ET_RUNTIME_NAMESPACE::indexToCoordinate; + using ::executorch::ET_RUNTIME_NAMESPACE::nonempty_size; + using ::executorch::ET_RUNTIME_NAMESPACE::nonzero_dim; diff --git a/patches/0006-Index-portable-kernels-by-the-tensor-strides.patch b/patches/0006-Index-portable-kernels-by-the-tensor-strides.patch new file mode 100644 index 0000000..556dff2 --- /dev/null +++ b/patches/0006-Index-portable-kernels-by-the-tensor-strides.patch @@ -0,0 +1,172 @@ +Backport of pytorch/executorch#21828, kernel changes only (its unit tests are +not built here). Remove when ExecuTorch is updated to a release that includes it. + +diff --git a/kernels/portable/cpu/op_flip.cpp b/kernels/portable/cpu/op_flip.cpp +index 41ec666..8a381fc 100644 +--- a/kernels/portable/cpu/op_flip.cpp ++++ b/kernels/portable/cpu/op_flip.cpp +@@ -21,22 +21,6 @@ bool check_flip_args(const Tensor& in, IntArrayRef dims, const Tensor& out) { + return check_dim_list_is_valid(in, dims); + } + +-size_t unflip_flat_ix(size_t ix, const Tensor& in, ArrayRef flip_dim) { +- size_t ix_coord[kTensorDimensionLimit]; +- indexToCoordinate(in, ix, ix_coord); +- +- size_t unflip_coord[kTensorDimensionLimit]; +- for (const auto d : c10::irange(in.dim())) { +- if (flip_dim[d]) { +- unflip_coord[d] = in.size(d) - ix_coord[d] - 1; +- } else { +- unflip_coord[d] = ix_coord[d]; +- } +- } +- +- return coordinateToIndex(in, unflip_coord); +-} +- + } // namespace + + Tensor& flip_out( +@@ -72,8 +56,22 @@ Tensor& flip_out( + const CTYPE* in_data = in.const_data_ptr(); + CTYPE* out_data = out.mutable_data_ptr(); + ++ const bool out_is_default = executorch::runtime::is_contiguous_dim_order( ++ out.dim_order().data(), out.dim_order().size()); ++ + for (const auto ix : c10::irange(in.numel())) { +- out_data[ix] = in_data[unflip_flat_ix(ix, in, flip_dim)]; ++ // @lint-ignore CLANGTIDY facebook-hte-CArray ++ size_t coord[kTensorDimensionLimit]; ++ indexToCoordinate(in, ix, coord); ++ ++ // @lint-ignore CLANGTIDY facebook-hte-CArray ++ size_t src_coord[kTensorDimensionLimit]; ++ for (const auto d : c10::irange(in.dim())) { ++ src_coord[d] = flip_dim[d] ? in.size(d) - coord[d] - 1 : coord[d]; ++ } ++ ++ out_data[out_is_default ? ix : coordinateToIndex(out, coord)] = ++ in_data[coordinateToIndex(in, src_coord)]; + } + }); + +diff --git a/kernels/portable/cpu/op_permute_copy.cpp b/kernels/portable/cpu/op_permute_copy.cpp +index 719f8fc..6343d1c 100644 +--- a/kernels/portable/cpu/op_permute_copy.cpp ++++ b/kernels/portable/cpu/op_permute_copy.cpp +@@ -71,8 +71,19 @@ Tensor& permute_copy_out( + const CTYPE* const in_data = in.const_data_ptr(); + CTYPE* const out_data = out.mutable_data_ptr(); + ++ const bool out_is_default = executorch::runtime::is_contiguous_dim_order( ++ out.dim_order().data(), out.dim_order().size()); ++ + for (const auto i : c10::irange(out.numel())) { +- out_data[i] = ++ size_t out_ix = i; ++ if (!out_is_default) { ++ // @lint-ignore CLANGTIDY facebook-hte-CArray ++ size_t out_coord[kTensorDimensionLimit]; ++ indexToCoordinate(out, i, out_coord); ++ out_ix = coordinateToIndex(out, out_coord); ++ } ++ ++ out_data[out_ix] = + in_data[executorch::runtime::coordinateToIndexWithTrailingDimsMemo( + in, in_coord, trailing_dims_memo)]; + increment_coordinate_permuted(in, in_coord, dims); +diff --git a/kernels/portable/cpu/op_roll.cpp b/kernels/portable/cpu/op_roll.cpp +index 4d314b3..c510aa9 100644 +--- a/kernels/portable/cpu/op_roll.cpp ++++ b/kernels/portable/cpu/op_roll.cpp +@@ -32,19 +32,6 @@ bool check_roll_args( + return true; + } + +-size_t unshift_flat_ix(size_t ix, const Tensor& in, IntArrayRef dim_shifts) { +- size_t ix_coord[kTensorDimensionLimit]; +- indexToCoordinate(in, ix, ix_coord); +- +- size_t shifted_coord[kTensorDimensionLimit]; +- for (const auto d : c10::irange(in.dim())) { +- shifted_coord[d] = +- (ix_coord[d] + in.size(d) - dim_shifts[d] % in.size(d)) % in.size(d); +- } +- +- return coordinateToIndex(in, shifted_coord); +-} +- + } // namespace + + Tensor& roll_out( +@@ -86,8 +73,23 @@ Tensor& roll_out( + const CTYPE* in_data = in.const_data_ptr(); + CTYPE* out_data = out.mutable_data_ptr(); + ++ const bool out_is_default = executorch::runtime::is_contiguous_dim_order( ++ out.dim_order().data(), out.dim_order().size()); ++ + for (const auto ix : c10::irange(out.numel())) { +- out_data[ix] = in_data[unshift_flat_ix(ix, in, dim_shifts)]; ++ // @lint-ignore CLANGTIDY facebook-hte-CArray ++ size_t coord[kTensorDimensionLimit]; ++ indexToCoordinate(in, ix, coord); ++ ++ // @lint-ignore CLANGTIDY facebook-hte-CArray ++ size_t shifted_coord[kTensorDimensionLimit]; ++ for (const auto d : c10::irange(in.dim())) { ++ shifted_coord[d] = ++ (coord[d] + in.size(d) - dim_shifts[d] % in.size(d)) % in.size(d); ++ } ++ ++ out_data[out_is_default ? ix : coordinateToIndex(out, coord)] = ++ in_data[coordinateToIndex(in, shifted_coord)]; + } + }); + +diff --git a/kernels/portable/cpu/op_scatter_add.cpp b/kernels/portable/cpu/op_scatter_add.cpp +index 690c313..e1cb5d8 100644 +--- a/kernels/portable/cpu/op_scatter_add.cpp ++++ b/kernels/portable/cpu/op_scatter_add.cpp +@@ -67,9 +67,6 @@ Tensor& scatter_add_out( + ET_KERNEL_CHECK( + ctx, tensors_have_same_dim_order(self, src, out), InvalidArgument, out); + +- ET_KERNEL_CHECK( +- ctx, tensor_is_default_dim_order(index), InvalidArgument, out); +- + if (dim < 0) { + dim += nonzero_dim(self); + } +diff --git a/runtime/core/exec_aten/util/tensor_util.h b/runtime/core/exec_aten/util/tensor_util.h +index 00ea924..fda71bc 100644 +--- a/runtime/core/exec_aten/util/tensor_util.h ++++ b/runtime/core/exec_aten/util/tensor_util.h +@@ -1025,8 +1025,9 @@ inline size_t coordinateToIndex( + const executorch::aten::Tensor& tensor, + const size_t* const coordinate) { + size_t index = 0; ++ const auto strides = tensor.strides(); + for (const auto d : c10::irange(tensor.dim())) { +- index += coordinate[d] * getTrailingDims(tensor, d); ++ index += coordinate[d] * static_cast(strides[d]); + } + return index; + } +@@ -1039,11 +1040,9 @@ inline size_t coordinateToIndex( + inline void memoizeTrailingDims( + const executorch::aten::Tensor& tensor, + size_t trailing_dims_memo[kTensorDimensionLimit]) { +- const auto tensorDim = tensor.dim(); +- size_t dims = 1; +- for (int ii = tensorDim - 1; ii >= 0; --ii) { +- trailing_dims_memo[ii] = dims; +- dims *= static_cast(tensor.size(ii)); ++ const auto strides = tensor.strides(); ++ for (const auto d : c10::irange(tensor.dim())) { ++ trailing_dims_memo[d] = static_cast(strides[d]); + } + } +