Skip to content

Commit cbe6c5c

Browse files
committed
[slimtensor] Add SlimTensor class with basic properties and CPU copy operation
**Key components:** 1. **`c10/core/Contiguity.h`** - Contiguity checking utility: - `_compute_contiguous<T>()` - computes whether a tensor with given sizes/strides is contiguous in memory (row-major order) 2. **`core/SlimTensor.h`** - Main SlimTensor class with: - **Constructors**: Default (undefined tensor) and full constructor with storage, sizes, strides, dtype, and storage_offset - **Property accessors**: - `sizes()`, `size(dim)` - get tensor dimensions with negative indexing support - `strides()`, `stride(dim)` - get tensor strides with negative indexing support - `dtype()`, `device()`, `device_type()`, `device_index()` - `numel()`, `dim()`, `nbytes()`, `itemsize()` - `data_ptr()` - returns pointer to tensor data (adjusted for storage_offset) - `storage_offset()`, `storage()` - **State queries**: `defined()`, `is_cpu()`, `is_contiguous()`, `is_empty()` - **Copy operation**: `copy_(other)` - copies data from another tensor - Fast path: uses memcpy for both-contiguous tensors - Slow path: element-wise copy respecting strides for non-contiguous tensors - **Setters**: `reset()`, `set_storage()`, `set_sizes_and_strides()` **Curretnt constraints:** - Only CPU device supported - Only Float32 dtype tested - copy_() only supports CPU-to-CPU copy Those contraints will be further improved in the following diffs Differential Revision: [D89750150](https://our.internmc.facebook.com/intern/diff/D89750150/) ghstack-source-id: 331051343 Pull Request resolved: #16385
1 parent f6a75a8 commit cbe6c5c

File tree

7 files changed

+1066
-0
lines changed

7 files changed

+1066
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#pragma once
10+
11+
#include <cstdint>
12+
13+
#include <executorch/runtime/core/array_ref.h>
14+
15+
namespace executorch::backends::aoti::slim::c10 {
16+
17+
using ::executorch::runtime::ArrayRef;
18+
19+
/**
20+
* Compute whether a tensor with given sizes, strides, and numel is contiguous.
21+
*
22+
* A tensor is contiguous if its elements are laid out in memory in row-major
23+
* order, i.e., the stride of the last dimension is 1, and each preceding
24+
* dimension's stride equals the product of all following dimensions' sizes.
25+
*
26+
* @param sizes The sizes of each dimension
27+
* @param strides The strides of each dimension
28+
* @param numel The total number of elements
29+
* @return true if the tensor is contiguous, false otherwise
30+
*/
31+
template <typename T>
32+
bool _compute_contiguous(ArrayRef<T> sizes, ArrayRef<T> strides, T numel) {
33+
if (numel == 0) {
34+
return true;
35+
}
36+
37+
T expected_stride = 1;
38+
// Iterate from last dimension to first
39+
for (int64_t d = static_cast<int64_t>(sizes.size()) - 1; d >= 0; d--) {
40+
const auto& size_d = sizes[d];
41+
if (size_d == 1) {
42+
// Size-1 dimensions don't affect contiguity
43+
continue;
44+
}
45+
46+
if (strides[d] != expected_stride) {
47+
return false;
48+
}
49+
expected_stride *= size_d;
50+
}
51+
return true;
52+
}
53+
54+
} // namespace executorch::backends::aoti::slim::c10

backends/aoti/slim/c10/core/targets.bzl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,24 @@ def define_common_targets():
5454
],
5555
)
5656

57+
# Header-only library for Contiguity
58+
runtime.cxx_library(
59+
name = "contiguity",
60+
headers = [
61+
"Contiguity.h",
62+
],
63+
visibility = ["@EXECUTORCH_CLIENTS"],
64+
exported_deps = [
65+
"//executorch/runtime/core:core",
66+
],
67+
)
68+
5769
# Combined c10 core library
5870
runtime.cxx_library(
5971
name = "core",
6072
visibility = ["@EXECUTORCH_CLIENTS"],
6173
exported_deps = [
74+
":contiguity",
6275
":device",
6376
":device_type",
6477
":scalar_type",

0 commit comments

Comments
 (0)