Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions backends/aoti/slim/c10/core/Contiguity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <cstdint>

#include <executorch/runtime/core/array_ref.h>

namespace executorch::backends::aoti::slim::c10 {

using ::executorch::runtime::ArrayRef;

/**
* Compute whether a tensor with given sizes, strides, and numel is contiguous.
*
* A tensor is contiguous if its elements are laid out in memory in row-major
* order, i.e., the stride of the last dimension is 1, and each preceding
* dimension's stride equals the product of all following dimensions' sizes.
*
* @param sizes The sizes of each dimension
* @param strides The strides of each dimension
* @param numel The total number of elements
* @return true if the tensor is contiguous, false otherwise
*/
template <typename T>
bool _compute_contiguous(ArrayRef<T> sizes, ArrayRef<T> strides, T numel) {
if (numel == 0) {
return true;
}

T expected_stride = 1;
// Iterate from last dimension to first
for (int64_t d = static_cast<int64_t>(sizes.size()) - 1; d >= 0; d--) {
const auto& size_d = sizes[d];
if (size_d == 1) {
// Size-1 dimensions don't affect contiguity
continue;
}

if (strides[d] != expected_stride) {
return false;
}
expected_stride *= size_d;
}
return true;
}

} // namespace executorch::backends::aoti::slim::c10
13 changes: 13 additions & 0 deletions backends/aoti/slim/c10/core/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,24 @@ def define_common_targets():
],
)

# Header-only library for Contiguity
runtime.cxx_library(
name = "contiguity",
headers = [
"Contiguity.h",
],
visibility = ["@EXECUTORCH_CLIENTS"],
exported_deps = [
"//executorch/runtime/core:core",
],
)

# Combined c10 core library
runtime.cxx_library(
name = "core",
visibility = ["@EXECUTORCH_CLIENTS"],
exported_deps = [
":contiguity",
":device",
":device_type",
":scalar_type",
Expand Down
Loading
Loading