[slimtensor] Add SlimTensor class with basic properties and CPU copy operation#16550
Merged
[slimtensor] Add SlimTensor class with basic properties and CPU copy operation#16550
Conversation
…inimal support Pull Request resolved: #16382 This diff introduces the foundational c10 core headers for SlimTensor, a lightweight tensor implementation used by torchnative, to cuda backend runtime and further it will be used by all aoti-driven backends like MPS. We add: - DeviceType.h - Device type enum (CPU only for now) - Device.h - Device class representing compute device location - ScalarType.h - Scalar type enum with elementSize() helper (Float only for now) These headers are modeled after PyTorch's c10 but simplified for our needs. The enum values are kept compatible with PyTorch for serialization compatibility. This is the first step in migrating SlimTensor to replace ETensor as the internal tensor representation in CUDA backend. Future diffs will add Storage, SlimTensor class, and additional dtypes/devices incrementally. ghstack-source-id: 332983719 @exported-using-ghexport Differential Revision: [D89747061](https://our.internmc.facebook.com/intern/diff/D89747061/)
Pull Request resolved: #16383 This diff introduces the foundation for tensor storage management in the SlimTensor migration: - util/SharedPtr.h - A lightweight, non-thread-safe shared pointer optimized for single-threaded tensor operations. - core/Storage.h - The MaybeOwningStorage class that manages tensor data memory: - DeviceTraits<CPU> specialization with allocate(), free(), memcpy() using malloc/free - Owning mode for CPU device (CUDA and non-owning mode added later) - Storage type alias as SharedPtr<MaybeOwningStorage> - Move semantics for efficient resource transfer - clone() and copy_() methods for data management ghstack-source-id: 332983720 @exported-using-ghexport Differential Revision: [D89747980](https://our.internmc.facebook.com/intern/diff/D89747980/)
…slimtensor usage Pull Request resolved: #16384 This diff adds the `SizesAndStrides` class for efficiently storing tensor dimension sizes and strides, along with utility functions for computing element counts and storage requirements. **Key components:** 1. **`c10/macros/Macros.h`** - Branch prediction hint macros (`SLIMTENSOR_LIKELY/UNLIKELY`) for optimizing hot paths 2. **`util/ArrayRefUtil.h`** - Re-exports ExecuTorch's `ArrayRef`, `IntArrayRef`, and `makeArrayRef`; adds `toVec()` utility for converting ArrayRef to std::vector 3. **`c10/core/SizesAndStrides.h`** - Packed container for tensor sizes and strides: - Inline storage for tensors up to 5 dimensions (no heap allocation) - Out-of-line heap storage for larger tensors - Full copy/move semantics with proper resource management - Equality operators and iterators - `std::initializer_list` overloads for convenient initialization 4. **`util/SizeUtil.h`** - Size computation utilities: - `compute_numel()` - compute total number of elements - `compute_contiguous_strides()` - compute row-major contiguous strides - `compute_storage_nbytes()` / `compute_storage_nbytes_contiguous()` - compute storage byte requirements ghstack-source-id: 332983721 @exported-using-ghexport Differential Revision: [D89749336](https://our.internmc.facebook.com/intern/diff/D89749336/)
…operation Pull Request resolved: #16385 **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 ghstack-source-id: 332983722 @exported-using-ghexport Differential Revision: [D89750150](https://our.internmc.facebook.com/intern/diff/D89750150/)
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/16550
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
Gasoonjia
approved these changes
Jan 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR was created by the merge bot to help merge the original PR into the main branch.
ghstack PR number: #16385 by @Gasoonjia
^ Please use this as the source of truth for the PR details, comments, and reviews
ghstack PR base: https://github.com/pytorch/executorch/tree/gh/gasoonjia/76/base
ghstack PR head: https://github.com/pytorch/executorch/tree/gh/gasoonjia/76/head
Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/gasoonjia/75/orig
Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/gasoonjia/76/orig
Differential Revision: D89750150
@diff-train-skip-merge